internal RefCommVector(MatrixBase M, RefVector v) : base(M, v) { m_Owner = v; // allocate send buffers foreach (int proc in Mtx._SpmvCommPattern.ComLists.Keys) { int len = Mtx._SpmvCommPattern.ComLists[proc].Length; IntPtr pBuffer = Marshal.AllocHGlobal(len * sizeof(double)); SendBuffers.Add(proc, pBuffer); SendBuffersLengths.Add(proc, len); } }
/// <summary> /// For each <em>j</em>, <br/> /// this[j] = this[j]*<paramref name="other"/>[j] /// </summary> /// <param name="other"></param> public override void MultiplyElementWise(VectorBase other) { if (!m_IsLocked) { throw new ApplicationException("works only in locked mode."); } if (!other.IsLocked) { throw new ArgumentException("other object must be locked.", "other"); } int N = this.m_Part.LocalLength; if (other.Part.LocalLength != N) { throw new ArgumentException("mismatch in vector size."); } RefVector o = (other as RefVector); for (int i = 0; i < N; i++) { this.m_Storage[i] *= o.m_Storage[i]; } }
override internal void SpMV_Local_Middle(double alpha, VectorBase a, double beta, VectorBase acc) { RefVector _a = a as RefVector; RefVector _acc = acc as RefVector; if (a == null) { throw new ArgumentException("a must be of type RefVector.", "a"); } if (acc == null) { throw new ArgumentException("acc must be of type RefVector.", "acc"); } //MatrixBase.CSR loaclPart = (MatrixBase.CSR)this.m_LocalMtx; int NoOfRows = m_RowPart.LocalLength; //int[] TouchCount = new int[NoOfRows]; // reference version: //{ // double[] _a_stor = _a.Storage; // double[] _acc_stor = _acc.Storage; // for (int Row = 0; Row < NoOfRows; Row++) { // int RowStart = loaclPart.RowStart[Row]; // int RowEnd = loaclPart.RowStart[Row + 1]; // double rowacc = 0; // for (int i = RowStart; i < RowEnd; i++) { // int Col = loaclPart.ColInd[i]; // double MtxEntry = loaclPart.Val[i]; // rowacc += MtxEntry * _a_stor[Col]; // } // _acc_stor[Row] = _acc_stor[Row] * beta + rowacc * alpha; // } //} //// test code //double[] a_clone = (double[])_a.Storage.Clone(); //double[] acc_clone = (double[])_acc.Storage.Clone(); // test code end // unsafe optimized version unsafe { double *_a_stor = _a.StorageAddr; double *_acc_stor = _acc.StorageAddr; int * _rowStart = this.LocalMatrixPin.pRowStart; int * _ColInd = this.LocalMatrixPin.pColInd; double *_val = this.LocalMatrixPin.pVal; double *MtxEntry = _val; for (int Row = 0; Row < NoOfRows; Row++) { int RowStart = _rowStart[Row]; int RowEnd = _rowStart[Row + 1]; double rowacc = 0; for (int i = RowStart; i < RowEnd; i++) { //float __MtxEntry = (float)*MtxEntry; // let's try if single prec would be sufficient //float __a_stor = (float)_a_stor[*_ColInd]; rowacc += *MtxEntry * _a_stor[*_ColInd]; //rowacc += (double)__MtxEntry * _a_stor[*_ColInd]; //TouchCount[*_ColInd]++; // diagnostics: how often is a[i] read ? _ColInd++; MtxEntry++; } _acc_stor[Row] = _acc_stor[Row] * beta + rowacc * alpha; } } //// Test code: test ok 05aug10, 16:36 //base.m_LocalMtx.RefSpMv(alpha, a_clone, beta, acc_clone); //double err = 0; //for (int i = 0; i < this.RowPart.LocalLength; i++) { // err += Math.Abs(acc_clone[i] - _acc.Storage[i]); //} //Console.WriteLine("err = " + err); //Console.WriteLine(); //// test code end; }
/// <summary> /// see <see cref="VectorBase.CopyPartlyFrom"/> /// </summary> public override void CopyPartlyFrom(VectorBase _src, int[] IdxThis, int PerThis, int[] IdxSrc, int PerSrc) { if (!m_IsLocked) { throw new ApplicationException("works only in locked mode."); } if (!_src.IsLocked) { throw new ArgumentException("other object must be locked.", "other"); } RefVector src = _src as RefVector; if (src == null) { throw new ArgumentException("type mismatch"); } if (PerThis <= 0) { throw new ArgumentOutOfRangeException("PerThis", "must be greater than 0."); } if (PerSrc <= 0) { throw new ArgumentOutOfRangeException("PerSrc", "must be greater than 0."); } int Nthis = this.m_Part.LocalLength; int Nsrc = src.m_Part.LocalLength; if (IdxThis.Length != IdxSrc.Length) { throw new ArgumentException("length of index arrays must be equal."); } int L = IdxThis.Length; if (Nthis % PerThis != 0) { throw new ArgumentException(""); } if (Nsrc % PerSrc != 0) { throw new ArgumentException(""); } int NPer = Nthis / PerThis; if (NPer != Nsrc / PerSrc) { throw new ArgumentException(""); } for (int n = 0; n < NPer; n++) { for (int l = 0; l < L; l++) { this.m_Storage[n * PerThis + IdxThis[l]] = src.m_Storage[n * PerSrc + IdxSrc[l]]; } } }