diff --git a/build/make.linux64.lib.py b/build/make.linux64.lib.py index efa6055..d798873 100644 --- a/build/make.linux64.lib.py +++ b/build/make.linux64.lib.py @@ -15,7 +15,7 @@ builddir = "./build_linux64" doinstall = True #copies the build_output to the install dir when finished cc = "g++" #compiler -cflags = "-fPIC" +cflags = "-fPIC -O3" libraries = "-l{}".format(libname) libdirs = "-L{} -L{}/lib".format(builddir,commondir) linkerflags = "-static -static-libgcc -Wl,-rpath=." diff --git a/build_linux64/libamscppnarray.linux64.a b/build_linux64/libamscppnarray.linux64.a index 7701ba9..9a39670 100644 Binary files a/build_linux64/libamscppnarray.linux64.a and b/build_linux64/libamscppnarray.linux64.a differ diff --git a/build_linux64/objstore/amscppnarray.o b/build_linux64/objstore/amscppnarray.o index b01ce7e..06f74ea 100644 Binary files a/build_linux64/objstore/amscppnarray.o and b/build_linux64/objstore/amscppnarray.o differ diff --git a/build_linux64/objstore/amscppnarray_inits.o b/build_linux64/objstore/amscppnarray_inits.o index a437bd7..6ce4664 100644 Binary files a/build_linux64/objstore/amscppnarray_inits.o and b/build_linux64/objstore/amscppnarray_inits.o differ diff --git a/build_linux64/objstore/amscppnarray_math.o b/build_linux64/objstore/amscppnarray_math.o index cf0b6f1..4a94787 100644 Binary files a/build_linux64/objstore/amscppnarray_math.o and b/build_linux64/objstore/amscppnarray_math.o differ diff --git a/build_linux64/objstore/amscppnarray_random.o b/build_linux64/objstore/amscppnarray_random.o index 2a748fc..5a8810a 100644 Binary files a/build_linux64/objstore/amscppnarray_random.o and b/build_linux64/objstore/amscppnarray_random.o differ diff --git a/build_linux64/tests b/build_linux64/tests index 152b659..f1d1844 100644 Binary files a/build_linux64/tests and b/build_linux64/tests differ diff --git a/include/amscppnarray/amscppnarray.hpp b/include/amscppnarray/amscppnarray.hpp index 996d635..735e378 100644 --- a/include/amscppnarray/amscppnarray.hpp +++ b/include/amscppnarray/amscppnarray.hpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include @@ -88,8 +89,17 @@ template class narray narray subarray(const narray_size_t ind1, const narray_size_t ind2) const; //permutations and sorting + - + //C++11 iterators + typedef T* iterator; + typedef const T* const_iterator; + iterator begin() {return data;} + iterator end() {return data + length;} + const_iterator begin() const {return data;} + const_iterator end() const {return data + length;} + const_iterator cbegin() const { return data; } + const_iterator cend() const { return data+length;} }; @@ -101,6 +111,8 @@ template narray ones(const narray_size_t N); narray linspacef(const float low, const float high, narray_size_t N); narray linspace(const double low, const double high, narray_size_t N); +//cast/conversion + ///////// //Tests// @@ -113,6 +125,8 @@ void test_narray3(); }; //end namespace narray }; //end namespace ams +#include + #include #include #include @@ -121,4 +135,6 @@ void test_narray3(); #include #include +#include + #endif \ No newline at end of file diff --git a/include/amscppnarray/amscppnarray_binrw.hpp b/include/amscppnarray/amscppnarray_binrw.hpp new file mode 100644 index 0000000..7b5d2b5 --- /dev/null +++ b/include/amscppnarray/amscppnarray_binrw.hpp @@ -0,0 +1,21 @@ +#ifndef __AMSCPPNARRAY_BINRW_HPP__ +#define __AMSCPPNARRAY_BINRW_HPP__ + +namespace ams +{ +namespace narray +{ + +//binary array read/write operations + +template int fwrite_ndarray(FILE *fp, narray *data, narray *shape); + +template int fread_ndarray(FILE *fp, narray *data, narray *shape); + + +}; +}; + +#include + +#endif \ No newline at end of file diff --git a/include/amscppnarray/amscppnarray_binrwimpl.hpp b/include/amscppnarray/amscppnarray_binrwimpl.hpp new file mode 100644 index 0000000..ec8c725 --- /dev/null +++ b/include/amscppnarray/amscppnarray_binrwimpl.hpp @@ -0,0 +1,153 @@ +#ifndef __AMSCPPNARRAY_BINRWIMPL_HPP__ +#define __AMSCPPNARRAY_BINRWIMPL_HPP__ + + + +namespace ams +{ +namespace narray +{ + +//binary array read/write operations + +template int fwrite_ndarray(FILE *fp, narray *data, narray *shape) +{ + int ret = narray_success; + narray_size_t piprod; + narray_size_t I; + int32_t Nd; + narray *lshape=NULL; + + if(fp==NULL) + { + ret = narray_failure; + printf("fwrite_ndarray: fp=NULL\n"); + return ret; + } + + if(shape==NULL) + { + lshape = new(std::nothrow) narray(); + lshape->resize(1); + lshape->data[0] = data->length; + } + else + { + lshape = shape; + } + + piprod = 1; + for(I=0;Ilength;I++) + { + if(lshape->data[I]>0) + { + piprod = piprod*lshape->data[I]; + } + else + { + piprod = 0; break; + } + } + + if(piprod!=data->length) + { + ret = narray_failure; + printf("fwrite_ndarray: buffer is size %ld, while shape is size %ld\n",(long) data->length,(long) piprod); + return ret; + } + + Nd = lshape->length; + + fwrite(&Nd,sizeof(int32_t),1,fp); + if(Nd>0) + { + fwrite(lshape->data,sizeof(int32_t),Nd,fp); + if(piprod>0) + { + fwrite(data->data,sizeof(T),data->length,fp); + } + } + + if(shape==NULL) + { + if(lshape!=NULL) {delete lshape; lshape=NULL;} + } + + + return ret; +} + +template int fread_ndarray(FILE *fp, narray *data, narray *shape) +{ + int ret = narray_success; + narray_size_t piprod; + narray_size_t I; + int32_t Nd; + narray *lshape=NULL; + + if(fp==NULL) + { + ret = narray_failure; + printf("fread_ndarray: fp=NULL\n"); + return ret; + } + + if(shape==NULL) + { + lshape = new(std::nothrow) narray(); + } + else + { + lshape = shape; + } + + + + fread(&Nd,sizeof(int32_t),1,fp); + if(Nd<=0) + { + data->resize(0); + lshape->resize(0); + ret = narray_failure; + return ret; + } + lshape->resize(Nd); + fread(lshape->data,sizeof(int32_t),Nd,fp); + + piprod = 1; + for(I=0;Ilength;I++) + { + if(lshape->data[I]>0) + { + piprod = piprod*lshape->data[I]; + } + else + { + piprod = 0; break; + } + } + + if(piprod<=0) + { + data->resize(0); + lshape->resize(0); + ret = narray_failure; + return ret; + } + + data->resize(piprod); + fread(data->data,sizeof(T),data->length,fp); + + if(shape==NULL) + { + if(lshape!=NULL) {delete lshape; lshape=NULL;} + } + + return ret; +} + + +}; +}; + +#endif \ No newline at end of file diff --git a/include/amscppnarray/amscppnarray_impl.hpp b/include/amscppnarray/amscppnarray_impl.hpp index 00d93e7..53c37e4 100644 --- a/include/amscppnarray/amscppnarray_impl.hpp +++ b/include/amscppnarray/amscppnarray_impl.hpp @@ -21,6 +21,7 @@ template int narray::resize(const narray_size_t newlength) int ret = narray_failure; narray_size_t I; T *newdata = NULL; + narray_size_t ml; if(newlength<=0) { @@ -37,14 +38,19 @@ template int narray::resize(const narray_size_t newlength) return ret; } - for(I=0;I(newdata,data,ml); + buffer_set(newdata,T(),length,newlength); + + // for(I=0;I narray::narray(const narray& other) int res; length = 0; data = NULL; + narray_size_t ml; //printf("debug: copy constructor called for\n\t%p\n\t%p\n", this, &other); @@ -72,10 +79,13 @@ template narray::narray(const narray& other) } else { - for(I=0;I(data,other.data,ml); + // for(I=0;I narray& narray::operator=(const narray& other) { narray_size_t I; int res; + narray_size_t ml; //printf("debug: operator= called for\n\t%p\n\t%p\n", this, &other); @@ -131,10 +142,13 @@ template narray& narray::operator=(const narray& other) } else { - for(I=0;I(data,other.data,ml); + // for(I=0;I void buffer_copycast(T1 *bufferto, const T2 *bufferfrom, const narray_size_t N); + + + template void buffer_set(T *buffer, const T val, const narray_size_t ind1, const narray_size_t ind2); + + + +}; +}; + +#include + +#endif \ No newline at end of file diff --git a/include/amscppnarray/amscppnarray_llbopsimpl.hpp b/include/amscppnarray/amscppnarray_llbopsimpl.hpp new file mode 100644 index 0000000..125dc49 --- /dev/null +++ b/include/amscppnarray/amscppnarray_llbopsimpl.hpp @@ -0,0 +1,166 @@ +#ifndef __AMSCPPNARRAY_LLBOPSIMPL_HPP__ +#define __AMSCPPNARRAY_LLBOPSIMPL_HPP__ + +namespace ams +{ +namespace narray +{ + +template void buffer_copycast_tf( + T1 *bufferto, + const T2 *bufferfrom, + const narray_size_t N, + int threadnum, int nthreads +) +{ + int I0,I1,Is,I; + + Is = N/nthreads; + I0 = Is*threadnum; + I1 = (threadnum>=(nthreads-1))? N: Is*(threadnum+1); + for(I=I0;I void buffer_copycast(T1 *bufferto, const T2 *bufferfrom, const narray_size_t N) +{ + narray_size_t I; + int J; + int nthreads; + std::vector threads; + + if(N<=0) return; + + if(Nnarray_max_threads) ? narray_max_threads : nthreads; + threads.resize(nthreads); + + for(J=0;J, + bufferto, + bufferfrom, + N, + J,nthreads + ); + if(threads[J]==NULL) + { + printf("buffer_copy_cast: warning, thread %d failed to launch.\n",J); + } + } + for(J=0;Jjoin(); + delete threads[J]; + threads[J]= NULL; + } + } + } + + return; +} + +template void buffer_set_tf( + T *bufferto, + const T val, + const narray_size_t ind1, + const narray_size_t ind2, + const narray_size_t N, + int threadnum, int nthreads +) +{ + int I0,I1,Is,I; + + Is = N/nthreads; + I0 = Is*threadnum; + I1 = (threadnum>=(nthreads-1))? N: Is*(threadnum+1); + + I0 = I0 + ind1; + I1 = I1 + ind1; + + for(I=I0;I void buffer_set(T *buffer, const T val, const narray_size_t ind1, const narray_size_t ind2) +{ + narray_size_t I; + int J; + int nthreads; + std::vector threads; + + narray_size_t N = ind2-ind1; + if(N<=0) return; + + if(Nnarray_max_threads) ? narray_max_threads : nthreads; + threads.resize(nthreads); + + for(J=0;J, + buffer, + val, + ind1,ind2,N, + J,nthreads + ); + if(threads[J]==NULL) + { + printf("buffer_copy_cast: warning, thread %d failed to launch.\n",J); + } + } + for(J=0;Jjoin(); + delete threads[J]; + threads[J]= NULL; + } + } + } + + return; +} + + +}; +}; + +#endif \ No newline at end of file diff --git a/include/amscppnarray/amscppnarray_math.hpp b/include/amscppnarray/amscppnarray_math.hpp index a6aa683..816d213 100644 --- a/include/amscppnarray/amscppnarray_math.hpp +++ b/include/amscppnarray/amscppnarray_math.hpp @@ -19,6 +19,7 @@ template narray narray_abs(narray &q); void narray_testmath1(); void narray_testmath2(); +void narray_testmath3(); }; //end namespaces }; diff --git a/include/amscppnarray/amscppnarray_mathimpl.hpp b/include/amscppnarray/amscppnarray_mathimpl.hpp index 943893b..74ecc96 100644 --- a/include/amscppnarray/amscppnarray_mathimpl.hpp +++ b/include/amscppnarray/amscppnarray_mathimpl.hpp @@ -101,8 +101,11 @@ namespace narray template double narray_mean(const narray &q) { double ret = 0.0; - T sum = narray_sum(q); - ret = (double)sum/((double)q.length); + if(q.length>0) + { + T sum = narray_sum(q); + ret = (double)sum/((double)q.length); + } return ret; } @@ -197,8 +200,12 @@ namespace narray template double narray_stdev(const narray &q) { - double ret = narray_var(q); - ret = ::sqrt(ret); + double ret = 0.0; + if(q.length>0) + { + ret = narray_var(q); + ret = ::sqrt(ret); + } return ret; } diff --git a/include/amscppnarray/amscppnarray_random.hpp b/include/amscppnarray/amscppnarray_random.hpp index b1c6300..8caa5e8 100644 --- a/include/amscppnarray/amscppnarray_random.hpp +++ b/include/amscppnarray/amscppnarray_random.hpp @@ -18,6 +18,8 @@ namespace rand static const narray_rands_t dpr32_mod = ( ((narray_rands_t)1) << ((narray_rands_t)30) ) - (narray_rands_t)1; static const narray_rands_t dpr32_mult1 = ( (narray_rands_t) 1201633 ); + static const narray_rands_t dpr32_add1 = ( (narray_rands_t) 293482 ); + extern narray_rands_t dpr32_rseed; // global random number seed for numeric array class narray_rands_t dpr32_nextseed(narray_rands_t seed); diff --git a/scripts/__pycache__/amsbinarrayrw.cpython-310.pyc b/scripts/__pycache__/amsbinarrayrw.cpython-310.pyc new file mode 100644 index 0000000..22f40f1 Binary files /dev/null and b/scripts/__pycache__/amsbinarrayrw.cpython-310.pyc differ diff --git a/scripts/amsbinarrayrw.py b/scripts/amsbinarrayrw.py new file mode 100644 index 0000000..96b7850 --- /dev/null +++ b/scripts/amsbinarrayrw.py @@ -0,0 +1,81 @@ +#!/usr/bin/python3 +import os,sys,math +import numpy as np + +#Write double ndarray +def fwrite_dndarray(fp,array): + + array = np.array(array,dtype=np.float64,order='F') + + Ndim = np.int32(len(array.shape)) + shp = np.int32(array.shape) + + q = Ndim.tobytes() + fp.write(q) + if(Ndim>0): + q = shp.tobytes() + fp.write(q) + + q = array.tobytes() + fp.write(q) + + return + +#Read double ndarray +def fread_dndarray(fp): + + array = np.zeros((),dtype=np.float64,order='F') + + q = fp.read(4) + Ndim = np.frombuffer(q,dtype=np.int32,count=1)[0] + + if(Ndim>0): + q = fp.read(4*Ndim) + shp = np.frombuffer(q,dtype=np.int32,count=Ndim) + piprod = 1 + for i in range(0,len(shp)): + piprod = piprod*shp[i] + q = fp.read(8*piprod) + array = np.frombuffer(q,dtype=np.float64,count=piprod) + array = array.reshape(shp,order='F') + + return array + +#Write float ndarray +def fwrite_fndarray(fp,array): + + array = np.array(array,dtype=np.float32,order='F') + + Ndim = np.int32(len(array.shape)) + shp = np.int32(array.shape) + + q = Ndim.tobytes() + fp.write(q) + if(Ndim>0): + q = shp.tobytes() + fp.write(q) + + q = array.tobytes() + fp.write(q) + + return + +#Read float ndarray +def fread_fndarray(fp): + + array = np.zeros((),dtype=np.float32,order='F') + + q = fp.read(4) + Ndim = np.frombuffer(q,dtype=np.int32,count=1)[0] + + if(Ndim>0): + q = fp.read(4*Ndim) + shp = np.frombuffer(q,dtype=np.int32,count=Ndim) + piprod = 1 + for i in range(0,len(shp)): + piprod = piprod*shp[i] + q = fp.read(4*piprod) + array = np.frombuffer(q,dtype=np.float32,count=piprod) + array = array.reshape(shp,order='F') + + return array \ No newline at end of file diff --git a/scripts/checkrandomness.py b/scripts/checkrandomness.py new file mode 100644 index 0000000..0c99dfa --- /dev/null +++ b/scripts/checkrandomness.py @@ -0,0 +1,31 @@ +#!/usr/bin/bash + +import os,sys,math +import numpy as np +import matplotlib.pyplot as plt + +from amsbinarrayrw import * + +def plot_random1(): + + fp = open("./build_linux64/tmp_testrand.bin","r+b") + data = fread_dndarray(fp) + N1 = data.shape[0] + N2 = int(math.sqrt(N1)) + N3 = N2*N2 + data = data[0:N3] + data = data.reshape([N2,N2]) + q = np.zeros([N2,N2,3]) + q[:,:,0] = data[:,:] + q[:,:,1] = data[:,:] + q[:,:,2] = data[:,:] + + hf = plt.figure(1) + plt.imshow(q) + plt.show() + fp.close() + +if(__name__=="__main__"): + plot_random1() + + pass \ No newline at end of file diff --git a/src/amscppnarray/amscppnarray_math.cpp b/src/amscppnarray/amscppnarray_math.cpp index 876ddd0..5e28bf0 100644 --- a/src/amscppnarray/amscppnarray_math.cpp +++ b/src/amscppnarray/amscppnarray_math.cpp @@ -57,15 +57,13 @@ namespace narray void narray_testmath2() { - rand::narray_rands_t rs = 1943981; + //rand::narray_rands_t rs = 1943981; rand::set_rseed_withtimer(); printf("rseed: %d\n",(int)rand::get_rseed()); printf("rseed: %d\n",(int)rand::dpr32_rseed); //< -- this is different than the line above - //rand::set_rseed(25); - printf("rseed: %d\n",(int)rand::get_rseed()); - narray unif = ams::narray::rand::narray_rand(5,&rs); + narray unif = ams::narray::rand::narray_rand(100); printf("rseed: %d\n",(int)rand::get_rseed()); @@ -94,5 +92,19 @@ namespace narray return; } + void narray_testmath3() + { + narray rb; + const char *fout = "./tmp_testrand.bin"; + FILE *fp = NULL; + fp = fopen(fout,"w+b"); + + rb = rand::narray_rand(500*500); + fwrite_ndarray(fp,&rb,NULL); + + fclose(fp); + + } + }; }; \ No newline at end of file diff --git a/src/amscppnarray/amscppnarray_random.cpp b/src/amscppnarray/amscppnarray_random.cpp index 225e445..85a8dad 100644 --- a/src/amscppnarray/amscppnarray_random.cpp +++ b/src/amscppnarray/amscppnarray_random.cpp @@ -13,16 +13,16 @@ namespace rand narray_rands_t dpr32_nextseed(narray_rands_t seed) { narray_rands_t sret = seed; - sret = ams::mod(sret*dpr32_mult1+1,dpr32_mod); + sret = ams::mod(sret*dpr32_mult1+dpr32_add1,dpr32_mod); return sret; } double dpr32_randd(narray_rands_t *seed) { double ret; - printf("debug: seed: %d\n",(int)*seed); + //printf("debug: seed: %d\n",(int)*seed); *seed = ams::narray::rand::dpr32_nextseed(*seed); - printf("debug: seed: %d\n",(int)*seed); + //printf("debug: seed: %d\n",(int)*seed); ret = (double)*seed/(double)(dpr32_mod-1); return ret; } diff --git a/src/main.cpp b/src/main.cpp index 90c4e6b..b316a02 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,10 +4,12 @@ int main(int argc, char* argv[]) { int ret = 0; printf("ams c++ numeric array library tests.\n"); - //ams::narray::test_narray1(); - //ams::narray::test_narray2(); - //ams::narray::test_narray3(); + // ams::narray::test_narray1(); + // ams::narray::test_narray2(); + // ams::narray::test_narray3(); + // ams::narray::narray_testmath1(); ams::narray::narray_testmath2(); + ams::narray::narray_testmath3(); return ret; } \ No newline at end of file