static PortableAbstractStream() { Type type = typeof(Buffer); const BindingFlags flags = BindingFlags.Static | BindingFlags.NonPublic; Type[] paramTypes = { typeof(byte*), typeof(byte*), typeof(int) }; // Assume .Net 4.5. MethodInfo mthd = type.GetMethod("Memcpy", flags, null, paramTypes, null); MemcpyInverted = true; if (mthd == null) { // Assume .Net 4.0. mthd = type.GetMethod("memcpyimpl", flags, null, paramTypes, null); MemcpyInverted = false; if (mthd == null) throw new InvalidOperationException("Unable to get memory copy function delegate."); } Memcpy = (MemCopy)Delegate.CreateDelegate(typeof(MemCopy), mthd); }
internal static void NpyArray_CopySwapFunc(VoidPtr dest, VoidPtr Source, bool swap, NpyArray arr) { if (arr == null) { return; } if (NpyArray_HASFIELDS(arr)) { NpyDict_KVPair KVPair = new NpyDict_KVPair(); NpyArray_Descr descr; NpyDict_Iter pos = new NpyDict_Iter(); descr = arr.descr; NpyDict_IterInit(pos); while (NpyDict_IterNext(descr.fields, pos, KVPair)) { NpyArray_DescrField value = KVPair.value as NpyArray_DescrField; string key = KVPair.key as string; if (null != value.title && key.CompareTo(value.title) != 0) { continue; } arr.descr = value.descr; MemCopy.GetMemcopyHelper(dest).copyswap(dest + value.offset, Source + value.offset, swap); } arr.descr = descr; return; } if (swap && arr.descr.subarray != null) { NpyArray_Descr descr = new NpyArray_Descr(NPY_TYPES.NPY_OBJECT); NpyArray_Descr newDescr = null; npy_intp num; int itemsize; descr = arr.descr; newDescr = descr.subarray._base; arr.descr = newDescr; itemsize = newDescr.elsize; num = descr.elsize / itemsize; _default_copyswap(dest, itemsize, Source, itemsize, num, swap, arr); arr.descr = descr; return; } if (Source != null) { memcpy(dest, Source, NpyArray_ITEMSIZE(arr)); } if (swap) { swapvalue(dest, NpyArray_ITEMSIZE(arr)); } return; }
static void _default_copyswap(VoidPtr dst, npy_intp dstride, VoidPtr src, npy_intp sstride, npy_intp n, bool swap, NpyArray arr) { if (src != null && dst.type_num != src.type_num) { VoidPtr _dst = new VoidPtr(dst); VoidPtr _src = new VoidPtr(src); for (npy_intp i = 0; i < n; i++) { NpyArray_CopySwapFunc(_dst, _src, swap, arr); if (swap) { swapvalue(_dst, arr.ItemSize); } _dst.data_offset += dstride; _src.data_offset += sstride; } return; } else { VoidPtr _dst = new VoidPtr(dst); VoidPtr _src = src != null ? new VoidPtr(src) : null; var helper = MemCopy.GetMemcopyHelper(_dst); helper.default_copyswap(_dst, dstride, _src, sstride, n, swap); } }
private static void _contig_to_contig(VoidPtr dst, npy_intp dst_stride, VoidPtr src, npy_intp src_stride, npy_intp N, npy_intp src_itemsize, NpyAuxData transferdata) { var helper = MemCopy.GetMemcopyHelper(dst); helper.memmove_init(dst, src); helper.memmove(dst.data_offset, src.data_offset, src_itemsize * N); }
static PortableAbstractStream() { Type type = typeof(Buffer); const BindingFlags flags = BindingFlags.Static | BindingFlags.NonPublic; Type[] paramTypes = { typeof(byte *), typeof(byte *), typeof(int) }; // Assume .Net 4.5. MethodInfo mthd = type.GetMethod("Memcpy", flags, null, paramTypes, null); MemcpyInverted = true; if (mthd == null) { // Assume .Net 4.0. mthd = type.GetMethod("memcpyimpl", flags, null, paramTypes, null); MemcpyInverted = false; if (mthd == null) { throw new InvalidOperationException("Unable to get memory copy function delegate."); } } Memcpy = (MemCopy)Delegate.CreateDelegate(typeof(MemCopy), mthd); }
internal static int NpyArray_FillWithScalar(NpyArray arr, NpyArray zero_d_array) { npy_intp size; NpyArray from; VoidPtr fromptr; size = NpyArray_SIZE(arr); if (size == 0) { return(0); } if (!NpyArray_ISALIGNED(zero_d_array) || zero_d_array.descr.type != arr.descr.type) { Npy_INCREF(arr.descr); from = NpyArray_FromArray(zero_d_array, arr.descr, NPYARRAYFLAGS.NPY_ALIGNED); if (from == null) { return(-1); } } else { from = zero_d_array; Npy_INCREF(from); } bool swap = (NpyArray_ISNOTSWAPPED(arr) != NpyArray_ISNOTSWAPPED(from)); fromptr = new VoidPtr(from); if (NpyArray_ISONESEGMENT(arr)) { VoidPtr toptr = new VoidPtr(arr); var helper = MemCopy.GetMemcopyHelper(toptr); helper.FillWithScalar(toptr, fromptr, size, swap); } else { NpyArrayIterObject iter; iter = NpyArray_IterNew(arr); if (iter == null) { Npy_DECREF(from); return(-1); } var helper = MemCopy.GetMemcopyHelper(iter.dataptr); helper.FillWithScalarIter(iter, fromptr, size, swap); Npy_DECREF(iter); } Npy_DECREF(from); return(0); }
internal static object DifferentSizes_GetItemFunc(npy_intp index, NpyArray npa) { // handles case of views mapped to different size arrays var DestArray = NpyDataMem_NEW(npa.ItemType, 1, false); MemCopy.MemCpy(DestArray, 0, npa.data, index, npa.ItemSize); return(GetIndex(DestArray, 0)); }
internal static int DifferentSizes_SetItemFunc(npy_intp index, dynamic value, NpyArray npa) { // handles case of views mapped to different size arrays var SrcArray = NpyDataMem_NEW(npa.ItemType, 1, false); SetIndex(SrcArray, 0, value); MemCopy.MemCpy(npa.data, index, SrcArray, 0, npa.ItemSize); return(1); }
internal static NpyArray NpyArray_GetMap(NpyArrayMapIterObject mit) { NpyArrayIterObject it; /* Unbound map iterator --- Bind should have been called */ if (mit.ait == null) { return(null); } /* This relies on the map iterator object telling us the shape * of the new array in nd and dimensions. */ NpyArray temp = mit.ait.ao; Npy_INCREF(temp.descr); NpyArray ret = NpyArray_Alloc(temp.descr, mit.nd, mit.dimensions, NpyArray_ISFORTRAN(temp), Npy_INTERFACE(temp)); if (ret == null) { return(null); } /* * Now just iterate through the new array filling it in * with the next object from the original array as * defined by the mapping iterator */ if ((it = NpyArray_IterNew(ret)) == null) { Npy_DECREF(ret); return(null); } bool swap = (NpyArray_ISNOTSWAPPED(temp) != NpyArray_ISNOTSWAPPED(ret)); NpyArray_MapIterReset(mit); var helper = MemCopy.GetMemcopyHelper(it.dataptr); helper.GetMap(it, mit, swap); Npy_DECREF(it); /* check for consecutive axes */ if ((mit.subspace != null) && (mit.consec != 0)) { if (mit.iteraxes[0] > 0) { /* then we need to swap */ _swap_axes(mit, ref ret, true); } } return(ret); }
private static VoidPtr ConvertAllToUInt64s(VoidPtr src, int srcOffset, int length) { int totalBytesToCopy = (length - srcOffset); UInt64[] destArray = new UInt64[(totalBytesToCopy + sizeof(UInt64) - 1) / sizeof(UInt64)]; VoidPtr Dest = new VoidPtr(destArray); MemCopy.MemCpy(Dest, 0, src, srcOffset, totalBytesToCopy); return(Dest); }
private static VoidPtr ConvertAllToFloats(VoidPtr src, int srcOffset, int length) { int totalBytesToCopy = (length - srcOffset); float[] destArray = new float[(totalBytesToCopy + sizeof(float) - 1) / sizeof(float)]; VoidPtr Dest = new VoidPtr(destArray); MemCopy.MemCpy(Dest, 0, src, srcOffset, totalBytesToCopy); return(Dest); }
private static VoidPtr ConvertAllToDecimals(VoidPtr src, int srcOffset, int length) { int totalBytesToCopy = (length - srcOffset); decimal[] destArray = new decimal[(totalBytesToCopy + sizeof(decimal) - 1) / sizeof(decimal)]; VoidPtr Dest = new VoidPtr(destArray); MemCopy.MemCpy(Dest, 0, src, srcOffset, totalBytesToCopy); return(Dest); }
public static VoidPtr NpyDataMem_RENEW(VoidPtr oldArray, ulong newSize) { VoidPtr newArray = NpyDataMem_NEW(oldArray.type_num, newSize, true); ulong preserveLength = System.Math.Min(VoidPointer_BytesLength(oldArray), newSize); if (preserveLength > 0) { MemCopy.MemCpy(newArray, 0, oldArray, 0, (long)preserveLength); } return(newArray); }
internal static void memmove(VoidPtr dest, npy_intp dest_offset, VoidPtr src, npy_intp src_offset, long len) { if (dest.type_num == NPY_TYPES.NPY_DECIMAL) { VoidPtr Temp = new VoidPtr(new decimal[len / sizeof(decimal)]); MemCopy.MemCpy(Temp, 0, src, src_offset, len); MemCopy.MemCpy(dest, dest_offset, Temp, 0, len); } else { VoidPtr Temp = new VoidPtr(new byte[len]); MemCopy.MemCpy(Temp, 0, src, src_offset, len); MemCopy.MemCpy(dest, dest_offset, Temp, 0, len); } }
internal static void memmove(VoidPtr dest, npy_intp dest_offset, VoidPtr src, npy_intp src_offset, long len) { if ((src.type_num == dest.type_num) && IsElementAligned(src, src_offset) && IsElementAligned(dest, dest_offset)) { var helper = MemCopy.GetMemcopyHelper(dest); helper.memmove(dest, dest_offset, src, src_offset, len); return; } else { VoidPtr Temp = new VoidPtr(new byte[len]); MemCopy.MemCpy(Temp, 0, src, src_offset, len); MemCopy.MemCpy(dest, dest_offset, Temp, 0, len); return; } }
internal static int NpyArray_SetMap(NpyArrayMapIterObject mit, NpyArray arr) { NpyArrayIterObject it; /* Unbound Map Iterator */ if (mit.ait == null) { return(-1); } Npy_INCREF(arr); if ((mit.subspace != null) && (mit.consec != 0)) { if (mit.iteraxes[0] > 0) { /* then we need to swap */ _swap_axes(mit, ref arr, false); if (arr == null) { return(-1); } } } /* Be sure values array is "broadcastable" * to shape of mit.dimensions, mit.nd */ if ((it = NpyArray_BroadcastToShape(arr, mit.dimensions, mit.nd)) == null) { Npy_DECREF(arr); return(-1); } bool swap = (NpyArray_ISNOTSWAPPED(mit.ait.ao) != NpyArray_ISNOTSWAPPED(arr)); NpyArray_MapIterReset(mit); var helper = MemCopy.GetMemcopyHelper(mit.dataptr); helper.SetMap(mit, it, swap); Npy_DECREF(arr); Npy_DECREF(it); return(0); }
private static void _strided_to_strided(VoidPtr dst, npy_intp dst_stride, VoidPtr src, npy_intp src_stride, npy_intp N, npy_intp src_itemsize, NpyAuxData data) { VoidPtr _dst = new VoidPtr(dst); VoidPtr _src = new VoidPtr(src); var helper = MemCopy.GetMemcopyHelper(_dst); helper.memmove_init(_dst, _src); while (N > 0) { helper.memmove(_dst.data_offset, _src.data_offset, src_itemsize); _dst.data_offset += dst_stride; _src.data_offset += src_stride; --N; } }
internal static void memclr(VoidPtr dest, npy_intp len) { var helper = MemCopy.GetMemcopyHelper(dest); helper.memclr(dest, dest.data_offset, len); }
static NpyArray _npyarray_correlate(NpyArray ap1, NpyArray ap2, NPY_TYPES typenum, NPY_CONVOLE_MODE mode, ref int inverted) { npy_intp n1 = NpyArray_DIM(ap1, 0); npy_intp n2 = NpyArray_DIM(ap2, 0); if (n1 < n2) { var temp = ap1; ap1 = ap2; ap2 = temp; temp = null; var t = n1; n1 = n2; n2 = t; inverted = 1; } else { inverted = 0; } npy_intp n_left, n_right; npy_intp[] length = new npy_intp[] { n1 }; npy_intp n = n2; switch (mode) { case NPY_CONVOLE_MODE.NPY_CONVOLVE_VALID: length[0] = length[0] - n + 1; n_left = n_right = 0; break; case NPY_CONVOLE_MODE.NPY_CONVOLVE_SAME: n_left = (npy_intp)(n / 2); n_right = n - n_left - 1; break; case NPY_CONVOLE_MODE.NPY_CONVOLVE_FULL: n_right = n - 1; n_left = n - 1; length[0] = length[0] + n - 1; break; default: NpyErr_SetString(npyexc_type.NpyExc_ValueError, "mode must be 0, 1, or 2"); return(null); } /* * Need to choose an output array that can hold a sum * -- use priority to determine which subtype. */ NpyArray ret = new_array_for_sum(ap1, ap2, 1, length, typenum); if (ret == null) { return(null); } npy_intp is1 = NpyArray_STRIDE(ap1, 0); npy_intp is2 = NpyArray_STRIDE(ap2, 0); VoidPtr op = new VoidPtr(ret); npy_intp os = NpyArray_ITEMSIZE(ret); VoidPtr ip1 = new VoidPtr(ap1); VoidPtr ip2 = new VoidPtr(ap2); ip2.data_offset += n_left * is2; n -= n_left; var helper = MemCopy.GetMemcopyHelper(ip1); helper.correlate(ip1, ip2, op, is1, is2, os, n, n1, n2, n_left, n_right); if (NpyErr_Occurred()) { goto clean_ret; } return(ret); clean_ret: Npy_DECREF(ret); return(null); }
internal static NpyArray NpyArray_MatrixProduct(NpyArray ap1, NpyArray ap2, NPY_TYPES typenum) { NpyArray ret = null; NpyArrayIterObject it1, it2; npy_intp i, j, l, is1, is2, os; npy_intp [] dimensions = new npy_intp[npy_defs.NPY_MAXDIMS]; int nd, axis, matchDim; VoidPtr op; if (ap2.nd > 1) { matchDim = ap2.nd - 2; } else { matchDim = 0; } l = ap1.dimensions[ap1.nd - 1]; if (ap2.dimensions[matchDim] != l) { NpyErr_SetString(npyexc_type.NpyExc_ValueError, "objects are not aligned"); return(null); } nd = ap1.nd + ap2.nd - 2; if (nd > npy_defs.NPY_MAXDIMS) { NpyErr_SetString(npyexc_type.NpyExc_ValueError, "dot: too many dimensions in result"); return(null); } j = 0; for (i = 0; i < ap1.nd - 1; i++) { dimensions[j++] = ap1.dimensions[i]; } for (i = 0; i < ap2.nd - 2; i++) { dimensions[j++] = ap2.dimensions[i]; } if (ap2.nd > 1) { dimensions[j++] = ap2.dimensions[ap2.nd - 1]; } is1 = ap1.strides[ap1.nd - 1]; is2 = ap2.strides[matchDim]; /* Choose which subtype to return */ ret = new_array_for_sum(ap1, ap2, nd, dimensions, typenum); if (ret == null) { return(null); } /* Ensure that multiarray.dot(<Nx0>,<0xM>) . zeros((N,M)) */ if (NpyArray_SIZE(ap1) == 0 && NpyArray_SIZE(ap2) == 0) { memset(NpyArray_DATA(ret), 0, (int)NpyArray_NBYTES(ret)); } else { /* Ensure that multiarray.dot([],[]) . 0 */ memset(NpyArray_DATA(ret), 0, (int)NpyArray_ITEMSIZE(ret)); } op = new VoidPtr(ret); os = NpyArray_ITEMSIZE(ret); axis = ap1.nd - 1; it1 = NpyArray_IterAllButAxis(ap1, ref axis); it2 = NpyArray_IterAllButAxis(ap2, ref matchDim); var helper = MemCopy.GetMemcopyHelper(it1.dataptr); helper.MatrixProduct(it1, it2, op, is1, is2, os, l); Npy_DECREF(it1); Npy_DECREF(it2); if (NpyErr_Occurred()) { goto fail; } return(ret); fail: Npy_XDECREF(ret); return(null); }
internal static NpyArray NpyArray_InnerProduct(NpyArray ap1, NpyArray ap2, NPY_TYPES typenum) { npy_intp [] dimensions = new npy_intp[npy_defs.NPY_MAXDIMS]; npy_intp l = ap1.dimensions[ap1.nd - 1]; if (ap2.dimensions[ap2.nd - 1] != l) { NpyErr_SetString(npyexc_type.NpyExc_ValueError, "matrices are not aligned"); return(null); } int nd = ap1.nd + ap2.nd - 2; int j = 0; for (int i = 0; i < ap1.nd - 1; i++) { dimensions[j++] = ap1.dimensions[i]; } for (int i = 0; i < ap2.nd - 1; i++) { dimensions[j++] = ap2.dimensions[i]; } /* * Need to choose an output array that can hold a sum * -- use priority to determine which subtype. */ NpyArray ret = new_array_for_sum(ap1, ap2, nd, dimensions, typenum); if (ret == null) { return(null); } npy_intp is1 = ap1.strides[ap1.nd - 1]; npy_intp is2 = ap2.strides[ap2.nd - 1]; VoidPtr op = new VoidPtr(ret); npy_intp os = ret.descr.elsize; int axis = ap1.nd - 1; NpyArrayIterObject it1 = NpyArray_IterAllButAxis(ap1, ref axis); axis = ap2.nd - 1; NpyArrayIterObject it2 = NpyArray_IterAllButAxis(ap2, ref axis); var helper = MemCopy.GetMemcopyHelper(it1.dataptr); helper.InnerProduct(it1, it2, op, is1, is2, os, l); Npy_DECREF(it1); Npy_DECREF(it2); if (NpyErr_Occurred()) { goto fail; } return(ret); fail: Npy_DECREF(ret); return(null); }
internal static void memcpy(VoidPtr dest, VoidPtr src, long len) { MemCopy.MemCpy(dest, 0, src, 0, len); }
internal static NpyArrayMapIterObject NpyArray_MapIterNew(NpyIndex [] indexes, int n) { NpyArrayMapIterObject mit; int i, j; /* Allocates the Python object wrapper around the map iterator. */ mit = new NpyArrayMapIterObject(); NpyObject_Init(mit, NpyArrayMapIter_Type); if (mit == null) { return(null); } for (i = 0; i < npy_defs.NPY_MAXDIMS; i++) { mit.iters[i] = null; } mit.index = 0; mit.ait = null; mit.subspace = null; mit.numiter = 0; mit.consec = 1; mit.n_indexes = 0; mit.nob_interface = null; /* Expand the boolean arrays in indexes. */ mit.n_indexes = NpyArray_IndexExpandBool(indexes, n, mit.indexes); if (mit.n_indexes < 0) { Npy_DECREF(mit); return(null); } /* Make iterators from any intp arrays and intp in the index. */ j = 0; for (i = 0; i < mit.n_indexes; i++) { NpyIndex index = mit.indexes[i]; if (index.type == NpyIndexType.NPY_INDEX_INTP_ARRAY) { mit.iters[j] = NpyArray_IterNew(index.intp_array); if (mit.iters[j] == null) { mit.numiter = j - 1; Npy_DECREF(mit); return(null); } j++; } else if (index.type == NpyIndexType.NPY_INDEX_INTP) { NpyArray_Descr indtype; NpyArray indarray; /* Make a 0-d array for the index. */ indtype = NpyArray_DescrFromType(NPY_TYPES.NPY_INTP); indarray = NpyArray_Alloc(indtype, 0, null, false, null); if (indarray == null) { mit.numiter = j - 1; Npy_DECREF(mit); return(null); } byte[] src = BitConverter.GetBytes(index.intp); var srcvp = new VoidPtr(src); var helper = MemCopy.GetMemcopyHelper(indarray.data); helper.memmove_init(indarray.data, srcvp); helper.memcpy(indarray.data.data_offset, srcvp.data_offset, sizeof(npy_intp)); mit.iters[j] = NpyArray_IterNew(indarray); Npy_DECREF(indarray); if (mit.iters[j] == null) { mit.numiter = j - 1; Npy_DECREF(mit); return(null); } j++; } } mit.numiter = j; /* Broadcast the index iterators. */ if (NpyArray_Broadcast(mit) < 0) { Npy_DECREF(mit); return(null); } return(mit); }
internal static NpyArray NpyArray_CopyAndTranspose(NpyArray arr) { NpyArray ret, tmp; int nd, eltsize; npy_intp stride2; npy_intp[] dims = new npy_intp[2]; npy_intp i, j; VoidPtr iptr; VoidPtr optr; /* make sure it is well-behaved */ tmp = NpyArray_ContiguousFromArray(arr, NpyArray_TYPE(arr)); if (tmp == null) { return(null); } arr = tmp; nd = NpyArray_NDIM(arr); if (nd == 1) { /* we will give in to old behavior */ Npy_DECREF(tmp); return(arr); } else if (nd != 2) { Npy_DECREF(tmp); NpyErr_SetString(npyexc_type.NpyExc_ValueError, "only 2-d arrays are allowed"); return(null); } /* Now construct output array */ dims[0] = NpyArray_DIM(arr, 1); dims[1] = NpyArray_DIM(arr, 0); eltsize = NpyArray_ITEMSIZE(arr); Npy_INCREF(arr.descr); ret = NpyArray_Alloc(arr.descr, 2, dims, false, null); if (ret == null) { Npy_DECREF(tmp); return(null); } /* do 2-d loop */ optr = new VoidPtr(ret); stride2 = eltsize * dims[0]; for (i = 0; i < dims[0]; i++) { iptr = new VoidPtr(arr); iptr.data_offset += i * eltsize; var helper = MemCopy.GetMemcopyHelper(optr); helper.memmove_init(optr, iptr); for (j = 0; j < dims[1]; j++) { /* optr[i,j] = iptr[j,i] */ helper.memcpy(optr.data_offset, iptr.data_offset, eltsize); optr.data_offset += eltsize; iptr.data_offset += stride2; } } Npy_DECREF(tmp); return(ret); }
internal static bool MemCpy(VoidPtr Dest, npy_intp DestOffset, VoidPtr Src, npy_intp SrcOffset, long Length) { return(MemCopy.MemCpy(Dest, DestOffset, Src, SrcOffset, Length)); }