#ifndef __AMSCPPNARRAY_IMPLOPER_HPP__ #define __AMSCPPNARRAY_IMPLOPER_HPP__ namespace ams { namespace narray { template void narray_operator_comp_tf( const narray *a, const narray *b, narray *cmp, int threadnum, int nthreads ) { int I0,I1,Is,I; Is = (nthreads<=1) ? a->length : a->length/nthreads; I0 = Is*threadnum; I1 = (threadnum>=(nthreads-1))? a->length: Is*(threadnum+1); cmp->data[threadnum] = 1; for(I=I0;Idata[I]!=b->data[I]) { cmp->data[threadnum] = 0; break; } } return; } template bool narray::operator==(const narray& other) const { bool ret = 1; narray cmp; narray_size_t I; int J; int nthreads; std::vector threads; if(this->length != other.length) { ret = 0; return ret; } if(this->length == 0) { ret = 1; return ret; } if(this->length < narray_thread_sz) { for(I=0;Ilength;I++) { if(this->data[I]!=other.data[I]) { ret = 0; break; } } } else { //threaded operation nthreads = std::thread::hardware_concurrency(); nthreads = (nthreads<1) ? 1 : nthreads; nthreads = (nthreads>narray_max_threads) ? narray_max_threads : nthreads; //if(nthreads<1) nthreads=1; //if(nthreads>narray_max_threads) nthreads = narray_max_threads; threads.resize(nthreads); cmp.resize(nthreads); for(J=0;J, this, &other, &cmp, J,nthreads ); } for(J=0;Jjoin(); delete threads[J]; threads[J]= NULL; } } ret = 1; for(J=0;J bool narray::operator!=(const narray& other) const { return !(*this==other); } template void narray_operator_add_tf( const narray *a, const narray *b, narray *out, int threadnum, int nthreads ) { int I0,I1,Is,I; Is = a->length/nthreads; I0 = Is*threadnum; I1 = (threadnum>=(nthreads-1))? a->length: Is*(threadnum+1); for(I=I0;Idata[I] = a->data[I] + b->data[I]; } return; } template narray narray::operator+(const narray& other) const { narray ret; narray_size_t I; int J; int nthreads; std::vector threads; if(this->length!=other.length) { ret.resize(0); printf("narray::operator+:: error - array sizes %ld and %ld are not the same.\n",(long)this->length,(long)other.length); } ret.resize(this->length); if(this->lengthlength;I++) ret.data[I] = this->data[I] + other.data[I]; } else { //threaded operation nthreads = std::thread::hardware_concurrency(); nthreads = (nthreads<1) ? 1 : nthreads; nthreads = (nthreads>narray_max_threads) ? narray_max_threads : nthreads; //if(nthreads<1) nthreads=1; //if(nthreads>narray_max_threads) nthreads = narray_max_threads; threads.resize(nthreads); for(J=0;J, this, &other, &ret, J,nthreads ); } for(J=0;Jjoin(); delete threads[J]; threads[J]= NULL; } } } return ret; } template void narray_operator_sub_tf( const narray *a, const narray *b, narray *out, int threadnum, int nthreads ) { int I0,I1,Is,I; Is = a->length/nthreads; I0 = Is*threadnum; I1 = (threadnum>=(nthreads-1))? a->length: Is*(threadnum+1); for(I=I0;Idata[I] = a->data[I] - b->data[I]; } return; } template narray narray::operator-(const narray& other) const { narray ret; narray_size_t I; int J; int nthreads; std::vector threads; if(this->length!=other.length) { ret.resize(0); printf("narray::operator-:: error - array sizes %ld and %ld are not the same.\n",(long)this->length,(long)other.length); } ret.resize(this->length); if(this->lengthlength;I++) ret.data[I] = this->data[I] - other.data[I]; } else { //threaded operation nthreads = std::thread::hardware_concurrency(); nthreads = (nthreads<1) ? 1 : nthreads; nthreads = (nthreads>narray_max_threads) ? narray_max_threads : nthreads; //if(nthreads<1) nthreads=1; //if(nthreads>narray_max_threads) nthreads = narray_max_threads; threads.resize(nthreads); for(J=0;J, this, &other, &ret, J,nthreads ); } for(J=0;Jjoin(); delete threads[J]; threads[J]= NULL; } } } return ret; } template void narray_operator_mult_tf( const narray *a, const narray *b, narray *out, int threadnum, int nthreads ) { int I0,I1,Is,I; Is = a->length/nthreads; I0 = Is*threadnum; I1 = (threadnum>=(nthreads-1))? a->length: Is*(threadnum+1); for(I=I0;Idata[I] = a->data[I] * b->data[I]; } return; } template narray narray::operator*(const narray& other) const { narray ret; narray_size_t I; int J; int nthreads; std::vector threads; if(this->length!=other.length) { ret.resize(0); printf("narray::operator*:: error - array sizes %ld and %ld are not the same.\n",(long)this->length,(long)other.length); } ret.resize(this->length); if(this->lengthlength;I++) ret.data[I] = this->data[I] * other.data[I]; } else { //threaded operation nthreads = std::thread::hardware_concurrency(); nthreads = (nthreads<1) ? 1 : nthreads; nthreads = (nthreads>narray_max_threads) ? narray_max_threads : nthreads; //if(nthreads<1) nthreads=1; //if(nthreads>narray_max_threads) nthreads = narray_max_threads; threads.resize(nthreads); for(J=0;J, this, &other, &ret, J,nthreads ); } for(J=0;Jjoin(); delete threads[J]; threads[J]= NULL; } } } return ret; } template void narray_operator_div_tf( const narray *a, const narray *b, narray *out, int threadnum, int nthreads ) { int I0,I1,Is,I; Is = a->length/nthreads; I0 = Is*threadnum; I1 = (threadnum>=(nthreads-1))? a->length: Is*(threadnum+1); for(I=I0;Idata[I] = a->data[I] / b->data[I]; } return; } template narray narray::operator/(const narray& other) const { narray ret; narray_size_t I; int J; int nthreads; std::vector threads; if(this->length!=other.length) { ret.resize(0); printf("narray::operator/:: error - array sizes %ld and %ld are not the same.\n",(long)this->length,(long)other.length); } ret.resize(this->length); if(this->lengthlength;I++) ret.data[I] = this->data[I] / other.data[I]; } else { //threaded operation nthreads = std::thread::hardware_concurrency(); nthreads = (nthreads<1) ? 1 : nthreads; nthreads = (nthreads>narray_max_threads) ? narray_max_threads : nthreads; //if(nthreads<1) nthreads=1; //if(nthreads>narray_max_threads) nthreads = narray_max_threads; threads.resize(nthreads); for(J=0;J, this, &other, &ret, J,nthreads ); } for(J=0;Jjoin(); delete threads[J]; threads[J]= NULL; } } } return ret; } template void narray_operator_add2_tf( const narray *a, const T val, narray *out, int threadnum, int nthreads ) { int I0,I1,Is,I; Is = a->length/nthreads; I0 = Is*threadnum; I1 = (threadnum>=(nthreads-1))? a->length: Is*(threadnum+1); for(I=I0;Idata[I] = a->data[I] + val; } return; } template narray narray::operator+(const T val) const { narray ret; narray_size_t I; int J; int nthreads; std::vector threads; ret.resize(this->length); if(this->lengthlength;I++) ret.data[I] = this->data[I] + val; } else { //threaded operation nthreads = std::thread::hardware_concurrency(); nthreads = (nthreads<1) ? 1 : nthreads; nthreads = (nthreads>narray_max_threads) ? narray_max_threads : nthreads; //if(nthreads<1) nthreads=1; //if(nthreads>narray_max_threads) nthreads = narray_max_threads; threads.resize(nthreads); for(J=0;J, this, val, &ret, J,nthreads ); } for(J=0;Jjoin(); delete threads[J]; threads[J]= NULL; } } } return ret; } template void narray_operator_sub2_tf( const narray *a, const T val, narray *out, int threadnum, int nthreads ) { int I0,I1,Is,I; Is = a->length/nthreads; I0 = Is*threadnum; I1 = (threadnum>=(nthreads-1))? a->length: Is*(threadnum+1); for(I=I0;Idata[I] = a->data[I] - val; } return; } template narray narray::operator-(const T val) const { narray ret; narray_size_t I; int J; int nthreads; std::vector threads; ret.resize(this->length); if(this->lengthlength;I++) ret.data[I] = this->data[I] - val; } else { //threaded operation nthreads = std::thread::hardware_concurrency(); nthreads = (nthreads<1) ? 1 : nthreads; nthreads = (nthreads>narray_max_threads) ? narray_max_threads : nthreads; //if(nthreads<1) nthreads=1; //if(nthreads>narray_max_threads) nthreads = narray_max_threads; threads.resize(nthreads); for(J=0;J, this, val, &ret, J,nthreads ); } for(J=0;Jjoin(); delete threads[J]; threads[J]= NULL; } } } return ret; } template void narray_operator_mult2_tf( const narray *a, const T val, narray *out, int threadnum, int nthreads ) { int I0,I1,Is,I; Is = a->length/nthreads; I0 = Is*threadnum; I1 = (threadnum>=(nthreads-1))? a->length: Is*(threadnum+1); for(I=I0;Idata[I] = a->data[I] * val; } return; } template narray narray::operator*(const T val) const { narray ret; narray_size_t I; int J; int nthreads; std::vector threads; ret.resize(this->length); if(this->lengthlength;I++) ret.data[I] = this->data[I] * val; } else { //threaded operation nthreads = std::thread::hardware_concurrency(); nthreads = (nthreads<1) ? 1 : nthreads; nthreads = (nthreads>narray_max_threads) ? narray_max_threads : nthreads; //if(nthreads<1) nthreads=1; //if(nthreads>narray_max_threads) nthreads = narray_max_threads; threads.resize(nthreads); for(J=0;J, this, val, &ret, J,nthreads ); } for(J=0;Jjoin(); delete threads[J]; threads[J]= NULL; } } } return ret; } template void narray_operator_div2_tf( const narray *a, const T val, narray *out, int threadnum, int nthreads ) { int I0,I1,Is,I; Is = a->length/nthreads; I0 = Is*threadnum; I1 = (threadnum>=(nthreads-1))? a->length: Is*(threadnum+1); for(I=I0;Idata[I] = a->data[I] / val; } return; } template narray narray::operator/(const T val) const { narray ret; narray_size_t I; int J; int nthreads; std::vector threads; ret.resize(this->length); if(this->lengthlength;I++) ret.data[I] = this->data[I] / val; } else { //threaded operation nthreads = std::thread::hardware_concurrency(); nthreads = (nthreads<1) ? 1 : nthreads; nthreads = (nthreads>narray_max_threads) ? narray_max_threads : nthreads; //if(nthreads<1) nthreads=1; //if(nthreads>narray_max_threads) nthreads = narray_max_threads; threads.resize(nthreads); for(J=0;J, this, val, &ret, J,nthreads ); } for(J=0;Jjoin(); delete threads[J]; threads[J]= NULL; } } } return ret; } template void narray_operator_negative_tf( const narray *a, narray *out, int threadnum, int nthreads ) { int I0,I1,Is,I; Is = a->length/nthreads; I0 = Is*threadnum; I1 = (threadnum>=(nthreads-1))? a->length: Is*(threadnum+1); for(I=I0;Idata[I] = -a->data[I]; } return; } template narray operator-(const narray &n) { narray ret; narray_size_t I; int J; int nthreads; std::vector threads; ret.resize(n.length); if(n.lengthnarray_max_threads) ? narray_max_threads : nthreads; //if(nthreads<1) nthreads=1; //if(nthreads>narray_max_threads) nthreads = narray_max_threads; threads.resize(nthreads); for(J=0;J, &n, &ret, J,nthreads ); } for(J=0;Jjoin(); delete threads[J]; threads[J]= NULL; } } } return ret; } template void narray_applyfn_tf( const narray *a, T (*fnptr)(T), narray *out, int threadnum, int nthreads ) { int I0,I1,Is,I; Is = a->length/nthreads; I0 = Is*threadnum; I1 = (threadnum>=(nthreads-1))? a->length: Is*(threadnum+1); for(I=I0;Idata[I] = fnptr(a->data[I]); } return; } template narray narray::applyfn(T (*fnptr)(T)) const { narray ret; narray_size_t I; int J; int nthreads; std::vector threads; ret.resize(this->length); if(this->lengthlength;I++) ret.data[I] = fnptr(this->data[I]); } else { //threaded operation nthreads = std::thread::hardware_concurrency(); nthreads = (nthreads<1) ? 1 : nthreads; nthreads = (nthreads>narray_max_threads) ? narray_max_threads : nthreads; //if(nthreads<1) nthreads=1; //if(nthreads>narray_max_threads) nthreads = narray_max_threads; threads.resize(nthreads); for(J=0;J, this, fnptr, &ret, J,nthreads ); } for(J=0;Jjoin(); delete threads[J]; threads[J]= NULL; } } } return ret; } template void narray_min_tf( const narray *in, narray *out, int threadnum, int nthreads ) { int I0,I1,Is,I; Is = in->length/nthreads; I0 = Is*threadnum; I1 = (threadnum>=(nthreads-1))? in->length: Is*(threadnum+1); for(I=I0;Idata[I]data[threadnum]) out->data[threadnum] = in->data[I]; } return; } template T narray::min() const { T ret = T(); narray minarr; narray_size_t I; int J; int nthreads; std::vector threads; if(this->length<=0) { return ret; } if(this->lengthdata[0]; for(I=0;Ilength;I++) { if(this->data[I]data[I]; } } } else { //threaded operation nthreads = std::thread::hardware_concurrency(); nthreads = (nthreads<1) ? 1 : nthreads; nthreads = (nthreads>narray_max_threads) ? narray_max_threads : nthreads; //if(nthreads<1) nthreads=1; //if(nthreads>narray_max_threads) nthreads = narray_max_threads; threads.resize(nthreads); minarr.resize(nthreads); minarr.setall(this->data[0]); for(J=0;J, this, &minarr, J,nthreads ); } for(J=0;Jjoin(); delete threads[J]; threads[J]= NULL; } } ret = this->data[0]; for(J=0;J void narray_max_tf( const narray *in, narray *out, int threadnum, int nthreads ) { int I0,I1,Is,I; Is = in->length/nthreads; I0 = Is*threadnum; I1 = (threadnum>=(nthreads-1))? in->length: Is*(threadnum+1); for(I=I0;Idata[I]>out->data[threadnum]) out->data[threadnum] = in->data[I]; } return; } template T narray::max() const { T ret = T(); narray maxarr; narray_size_t I; int J; int nthreads; std::vector threads; if(this->length<=0) { return ret; } if(this->lengthdata[0]; for(I=0;Ilength;I++) { if(this->data[I]>ret) { ret = this->data[I]; } } } else { //threaded operation nthreads = std::thread::hardware_concurrency(); nthreads = (nthreads<1) ? 1 : nthreads; nthreads = (nthreads>narray_max_threads) ? narray_max_threads : nthreads; //if(nthreads<1) nthreads=1; //if(nthreads>narray_max_threads) nthreads = narray_max_threads; threads.resize(nthreads); maxarr.resize(nthreads); maxarr.setall(this->data[0]); for(J=0;J, this, &maxarr, J,nthreads ); } for(J=0;Jjoin(); delete threads[J]; threads[J]= NULL; } } ret = this->data[0]; for(J=0;Jret) { ret = maxarr[J]; } } } return ret; } }; //end namespace narray }; //end namespace ams #endif