This commit is contained in:
madrocketsci committed 2025-06-22 16:48:23 -04:00
commit cad98f8211
64 files changed
+5157

No files matched your search

+170
View File
@@ -0,0 +1,170 @@
#ifndef __AMSCPPNARRAY_HPP__
#define __AMSCPPNARRAY_HPP__
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <stdint.h>
#include <vector>
#include <new>
#include <initializer_list>
#include <thread>
#include <algorithm>
#include <amsmathutil25/amsmathutil25.hpp>
namespace ams
{
namespace narray
{
// Forward declaration of the operator- template function
//template<typename T> class narray;
//template<typename T> narray<T> operator-(const narray<T> &n);
typedef int64_t narray_size_t;
static const int narray_success = 1;
static const int narray_failure = -1;
//problem size at which to begin using threaded operations
static const narray_size_t narray_thread_sz = 5000;
//maximum number of threads to use
static const int narray_max_threads = 50;
template<typename T> class narray
{
public:
narray_size_t length;
T *data;
narray();
narray(const narray<T>& other);
narray(narray<T>&& other) noexcept;
narray<T>& operator=(const narray<T>& other);
narray<T>& operator=(narray<T>&& other) noexcept;
narray<T>& operator=(const std::vector<T>& other);
narray(const std::initializer_list<T> initlist);
narray<T>& operator=(const std::initializer_list<T> initlist);
~narray();
template<typename T2> operator narray<T2>() const; //casting datatypes
int resize(const narray_size_t newlength);
const narray_size_t size() const;
T& operator[](const narray_size_t ind);
const T& operator[](const narray_size_t ind) const;
T& at(const narray_size_t ind);
const T& at(const narray_size_t ind) const;
void clear(); //erases all elements, sets size to 0
void setall(const T& val); //sets all elements to val
//Comparators
bool operator==(const narray<T>& other) const;
bool operator!=(const narray<T>& other) const;
//Operations
narray<T> operator+(const narray<T>& other) const;
narray<T> operator-(const narray<T>& other) const;
narray<T> operator*(const narray<T>& other) const;
narray<T> operator/(const narray<T>& other) const;
narray<T> operator+(const T val) const;
narray<T> operator-(const T val) const;
narray<T> operator*(const T val) const;
narray<T> operator/(const T val) const;
template<class U> friend narray<U> operator-(const narray<U> &n);
narray<T> applyfn(T (*fnptr)(T)) const;
//min,max
T min() const;
T max() const;
//insertions and deletions
// naive, assumes no ordering to the array
//inserts a value at (before) index ind
//insert(0,val) would give {val, oldval0, oldval1, ...}
//insert(N,val)
int insert(const narray_size_t ind, const T val);
//removes the value at index ind, and reduces array size by 1
int erase(const narray_size_t ind);
//finds the first instance of val in the array
narray_size_t find(const T val);
//finds the next instance of val, starting consideration at indstart
narray_size_t findnext(const narray_size_t indstart, const T val);
//finds where to insert a particular value such that the list remains ordered
narray_size_t find_insert_ordered(const T val);
//subarray operations
narray<T> 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;}
//printf (must be template specialized)
void print(int printstyle = 0);
};
//Initializers
template<typename T> narray<T> arange(const T low, const T high, const T incr);
template<typename T> narray<T> zeros(const narray_size_t N);
template<typename T> narray<T> ones(const narray_size_t N);
narray<float> linspacef(const float low, const float high, narray_size_t N);
narray<double> linspace(const double low, const double high, narray_size_t N);
//cast/conversion
/////////
//Tests//
/////////
void test_narray1();
void test_narray2();
void test_narray3();
void test_insertdelete1();
}; //end namespace narray
}; //end namespace ams
#include <amscppnarray/amscppnarray_llbops.hpp>
#include <amscppnarray/amscppnarray_impl.hpp>
#include <amscppnarray/amscppnarray_imploper.hpp>
#include <amscppnarray/amscppnarray_implsubarray.hpp>
#include <amscppnarray/amscppnarray_implinits.hpp>
#include <amscppnarray/amscppnarray_random.hpp>
#include <amscppnarray/amscppnarray_math.hpp>
#include <amscppnarray/amscppnarray_binrw.hpp>
#endif
@@ -0,0 +1,21 @@
#ifndef __AMSCPPNARRAY_BINRW_HPP__
#define __AMSCPPNARRAY_BINRW_HPP__
namespace ams
{
namespace narray
{
//binary array read/write operations
template<typename T> int fwrite_ndarray(FILE *fp, narray<T> *data, narray<int32_t> *shape);
template<typename T> int fread_ndarray(FILE *fp, narray<T> *data, narray<int32_t> *shape);
};
};
#include <amscppnarray/amscppnarray_binrwimpl.hpp>
#endif
@@ -0,0 +1,153 @@
#ifndef __AMSCPPNARRAY_BINRWIMPL_HPP__
#define __AMSCPPNARRAY_BINRWIMPL_HPP__
namespace ams
{
namespace narray
{
//binary array read/write operations
template<typename T> int fwrite_ndarray(FILE *fp, narray<T> *data, narray<int32_t> *shape)
{
int ret = narray_success;
narray_size_t piprod;
narray_size_t I;
int32_t Nd;
narray<int32_t> *lshape=NULL;
if(fp==NULL)
{
ret = narray_failure;
printf("fwrite_ndarray: fp=NULL\n");
return ret;
}
if(shape==NULL)
{
lshape = new(std::nothrow) narray<int32_t>();
lshape->resize(1);
lshape->data[0] = data->length;
}
else
{
lshape = shape;
}
piprod = 1;
for(I=0;I<lshape->length;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<typename T> int fread_ndarray(FILE *fp, narray<T> *data, narray<int32_t> *shape)
{
int ret = narray_success;
narray_size_t piprod;
narray_size_t I;
int32_t Nd;
narray<int32_t> *lshape=NULL;
if(fp==NULL)
{
ret = narray_failure;
printf("fread_ndarray: fp=NULL\n");
return ret;
}
if(shape==NULL)
{
lshape = new(std::nothrow) narray<int32_t>();
}
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;I<lshape->length;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
+524
View File
@@ -0,0 +1,524 @@
#ifndef __AMSCPPNARRAY_IMPL_HPP__
#define __AMSCPPNARRAY_IMPL_HPP__
namespace ams
{
namespace narray
{
template<typename T> narray<T>::narray()
{
//printf("debug: constructor called for %p.\n",this);
length = 0;
data = NULL;
return;
}
template<typename T> int narray<T>::resize(const narray_size_t newlength)
{
int ret = narray_failure;
narray_size_t I;
T *newdata = NULL;
narray_size_t ml;
if(newlength<=0)
{
if(data!=NULL) {delete[] data; data=NULL;}
length = 0;
ret = narray_success;
return ret;
}
newdata = new(std::nothrow) T[newlength];
if(newdata==NULL)
{
ret = narray_failure;
return ret;
}
//threaded initialization for speed
ml = (length<newlength) ? length : newlength;
buffer_copycast<T,T>(newdata,data,ml);
buffer_set<T>(newdata,T(),length,newlength);
// for(I=0;I<length&I<newlength;I++)
// {
// newdata[I] = data[I];
// }
// for(I=length;I<newlength;I++)
// {
// newdata[I] = T();
// }
if(data!=NULL) {delete[] data; data=NULL;}
data = newdata;
length = newlength;
ret = narray_success;
return ret;
}
template<typename T> narray<T>::narray(const narray<T>& other)
{
narray_size_t I;
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);
if(this!=&other)
{
res = this->resize(other.length);
if(res!=narray_success)
{
return;
}
else
{
//threaded copy for speed
ml = (length<other.length) ? length : other.length;
buffer_copycast<T,T>(data,other.data,ml);
// for(I=0;I<length && I<other.length;I++)
// {
// data[I] = other.data[I];
// }
}
}
}
template<typename T> narray<T>::narray(narray<T>&& other) noexcept
{
//this is a move operator
//I still need to understand when these are called.
//printf("debug: move constructor called for\n\t%p\n\t%p\n", this, &other);
//steal the resources from other and reset other to prevent double deletion
length = other.length;
data = other.data;
other.length = 0;
other.data = NULL;
return;
}
template<typename T> narray<T>& narray<T>::operator=(narray<T>&& other) noexcept
{
//printf("debug: move= called for\n\t%p\n\t%p\n", this, &other);
if(this!=&other)
{
if(data!=NULL) {delete[] data; data=NULL;}
length = other.length;
data = other.data;
other.length = 0;
other.data = NULL;
}
return *this;
}
template<typename T> narray<T>& narray<T>::operator=(const narray<T>& 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);
if(this!=&other)
{
res = this->resize(other.length);
if(res!=narray_success)
{
return *this;
}
else
{
//threaded copy for speed
ml = (length<other.length) ? length : other.length;
buffer_copycast<T,T>(data,other.data,ml);
// for(I=0;I<length && I<other.length;I++)
// {
// data[I] = other.data[I];
// }
}
}
return *this;
}
template<typename T> narray<T>& narray<T>::operator=(const std::vector<T>& other)
{
narray_size_t I;
int res;
if(this!=&other)
{
res = this->resize(other.size());
if(res!=narray_success)
{
return *this;
}
else
{
for(I=0;I<length && I<other.size();I++)
{
data[I] = other[I];
}
}
}
return *this;
}
template<typename T> narray<T>::narray(const std::initializer_list<T> initlist)
{
narray_size_t I;
int res;
length = 0;
data = NULL;
res = this->resize(initlist.size());
if(res!=narray_success)
{
return;
}
else
{
I = 0;
for(T elem : initlist)
{
data[I] = elem;
if(I<length) I++;
}
}
}
template<typename T> narray<T>& narray<T>::operator=(const std::initializer_list<T> initlist)
{
narray_size_t I;
int res;
res = this->resize(initlist.size());
if(res!=narray_success)
{
this->length = 0;
if(this->data != NULL) {delete[] this->data; this->data=NULL;}
return *this;
}
else
{
I = 0;
for(T elem : initlist)
{
data[I] = elem;
if(I<length) I++;
}
}
return *this;
}
template<class T> template<class T2> narray<T>::operator narray<T2>() const
{
//casting datatypes
narray<T2> ret;
int res;
res = ret.resize(this->length);
if(res!=narray_success)
{
ret.resize(0);
return ret;
}
else
{
buffer_copycast<T2,T>(ret.data,data,length);
}
return ret;
}
template<typename T> narray<T>::~narray()
{
//printf("debug: destructor called for %p\n",this);
length = 0;
if(data!=NULL) {delete[] data; data=NULL;}
return;
}
template<typename T> T& narray<T>::operator[](const narray_size_t ind)
{
return data[ind];
}
template<typename T> const T& narray<T>::operator[](const narray_size_t ind) const
{
return data[ind];
}
template<typename T> T& narray<T>::at(const narray_size_t ind)
{
return data[ind];
}
template<typename T> const T& narray<T>::at(const narray_size_t ind) const
{
return data[ind];
}
template<typename T> void narray<T>::clear()
{
this->resize(0);
return;
}
template<typename T> void narray_setall_tf(narray<T> *q, const T& val, int threadnum, int nthreads)
{
int I0,I1,Is,I;
Is = q->length/nthreads;
I0 = Is*threadnum;
I1 = (threadnum>=(nthreads-1))? q->length: Is*(threadnum+1);
for(I=I0;I<I1;I++)
{
q->data[I] = val;
}
return;
}
template<typename T> void narray<T>::setall(const T& val)
{
narray_size_t I;
int J;
int nthreads;
std::vector<std::thread*> threads;
if(length<narray_thread_sz)
{
for(I=0;I<length;I++) data[I] = val;
}
else
{
//threaded operation
nthreads = std::thread::hardware_concurrency();
if(nthreads<1) nthreads=1;
if(nthreads>narray_max_threads) nthreads = narray_max_threads;
threads.resize(nthreads);
for(J=0;J<nthreads;J++)
{
threads[J] = new(std::nothrow) std::thread(
&narray_setall_tf<T>,this,val,J,nthreads
);
}
for(J=0;J<nthreads;J++)
{
if(threads[J]!=NULL)
{
threads[J]->join();
delete threads[J];
threads[J]= NULL;
}
}
}
return;
}
template<typename T> const narray_size_t narray<T>::size() const
{
return length;
}
//inserts a value at (before) index ind
//insert(0,val) would give {val, oldval0, oldval1, ...}
//insert(N,val)
template<typename T> int narray<T>::insert(const narray_size_t ind, const T val)
{
int ret = narray_success;
int res;
narray<T> narr;
narray_size_t I;
if(ind<0)
{
ret = narray_failure;
return ret;
}
else if(ind<=this->length)
{
res = narr.resize(this->length+1);
if(res!=narray_success)
{
ret = narray_failure;
return ret;
}
if(this->data!=NULL)
{
for(I=0;I<ind && I<this->length;I++)
{
narr.data[I] = this->data[I];
}
}
narr.data[ind] = val;
if(this->data!=NULL)
{
for(I=ind+1;I<narr.length;I++)
{
narr.data[I] = this->data[I-1];
}
}
//steal data from narr
if(this->data!=NULL) {delete[] this->data; this->data=NULL;}
this->length = narr.length;
this->data = narr.data;
narr.length = 0;
narr.data = NULL;
}
else
{
//inserting past the end of the array
res = narr.resize(ind+1);
if(res!=narray_success)
{
ret = narray_failure;
return ret;
}
if(this->data!=NULL)
{
for(I=0;I<this->length;I++)
{
narr.data[I] = this->data[I];
}
}
narr.data[ind] = val;
//steal data from narr
if(this->data!=NULL) {delete[] this->data; this->data=NULL;}
this->length = narr.length;
this->data = narr.data;
narr.length = 0;
narr.data = NULL;
}
return ret;
}
//removes the value at index ind, and reduces array size by 1
template<typename T> int narray<T>::erase(const narray_size_t ind)
{
int ret = narray_success;
int res;
narray<T> narr;
narray_size_t I;
if(ind<0 || ind>=this->length)
{
ret = narray_failure;
return ret;
}
res = narr.resize(this->length-1);
if(res!=narray_success)
{
ret = narray_failure;
return ret;
}
if(this->data!=NULL)
{
for(I=0;I<ind && I<this->length && I<narr.length;I++)
{
narr.data[I] = this->data[I];
}
for(I=ind+1;I<this->length && (I-1)<narr.length;I++)
{
narr.data[I-1] = this->data[I];
}
}
//steal data from narr (should be similar to what std::move would do?)
if(this->data!=NULL) {delete[] this->data; this->data=NULL;}
this->length = narr.length;
this->data = narr.data;
narr.length = 0;
narr.data = NULL;
return ret;
}
//finds the first instance of val in the array
template<typename T> narray_size_t narray<T>::find(const T val)
{
narray_size_t ret = -1;
narray_size_t I;
for(I=0;I<this->length;I++)
{
if(this->data[I]==val)
{
ret = I;
break;
}
}
return ret;
}
//finds the next instance of val, starting consideration at indstart
template<typename T> narray_size_t narray<T>::findnext(const narray_size_t indstart, const T val)
{
narray_size_t ret = -1;
narray_size_t I;
for(I=indstart;I<this->length;I++)
{
if(this->data[I]==val)
{
ret = I;
break;
}
}
return ret;
}
//finds where to insert a particular value such that the list remains ordered
template<typename T> narray_size_t narray<T>::find_insert_ordered(const T val)
{
narray_size_t ret = -1;
narray_size_t I;
ret = 0;
for(I=0;I<this->length;I++)
{
ret = I+1;
if(this->data[I]>val)
{
ret = I;
break;
}
}
ret = (ret<0) ? 0 : ret;
ret = (ret>this->length) ? this->length : ret;
return ret;
}
}; //end namespace narray
}; //end namespace ams
#endif
@@ -0,0 +1,108 @@
#ifndef __AMSCPPNARRAY_IMPLINITS_HPP__
#define __AMSCPPNARRAY_IMPLINITS_HPP__
namespace ams
{
namespace narray
{
template<typename T> void narray_arange_tf(
narray<T> *in,
const T low, const T high, const T incr,
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;I<I1;I++)
{
in->data[I] = low + incr*I;
}
}
template<typename T> narray<T> arange(const T low, const T high, const T incr)
{
narray<T> ret;
narray_size_t I;
int J;
int nthreads;
std::vector<std::thread*> threads;
narray_size_t N = (narray_size_t)((high-low)/incr);
if(N<=0)
{
N = 0;
}
ret.resize(N);
if(ret.length==0)
{
return ret;
}
if(ret.length<narray_thread_sz)
{
for(I=0;I<ret.length;I++)
{
ret.data[I] = low + incr*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<nthreads;J++)
{
threads[J] = new(std::nothrow) std::thread(
&narray_arange_tf<T>,
&ret,
low,high,incr,
J,nthreads
);
}
for(J=0;J<nthreads;J++)
{
if(threads[J]!=NULL)
{
threads[J]->join();
delete threads[J];
threads[J]= NULL;
}
}
}
return ret;
}
template<typename T> narray<T> zeros(const narray_size_t N)
{
narray<T> ret;
ret.resize(N);
ret.setall(0);
return ret;
}
template<typename T> narray<T> ones(const narray_size_t N)
{
narray<T> ret;
ret.resize(N);
ret.setall(1);
return ret;
}
};
};
#endif
@@ -0,0 +1,977 @@
#ifndef __AMSCPPNARRAY_IMPLOPER_HPP__
#define __AMSCPPNARRAY_IMPLOPER_HPP__
namespace ams
{
namespace narray
{
template<typename T> void narray_operator_comp_tf(
const narray<T> *a,
const narray<T> *b,
narray<bool> *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;I<I1;I++)
{
if(a->data[I]!=b->data[I])
{
cmp->data[threadnum] = 0;
break;
}
}
return;
}
template<typename T> bool narray<T>::operator==(const narray<T>& other) const
{
bool ret = 1;
narray<bool> cmp;
narray_size_t I;
int J;
int nthreads;
std::vector<std::thread*> 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;I<this->length;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<nthreads;J++)
{
//printf("debug: launching thread %d\n",J);
threads[J] = new(std::nothrow) std::thread(
&narray_operator_comp_tf<T>,
this,
&other,
&cmp,
J,nthreads
);
}
for(J=0;J<nthreads;J++)
{
if(threads[J]!=NULL)
{
threads[J]->join();
delete threads[J];
threads[J]= NULL;
}
}
ret = 1;
for(J=0;J<nthreads;J++)
{
if(cmp[J]==0)
{
ret = 0;
break;
}
}
cmp.resize(0);
}
return ret;
}
template<typename T> bool narray<T>::operator!=(const narray<T>& other) const
{
return !(*this==other);
}
template<typename T> void narray_operator_add_tf(
const narray<T> *a,
const narray<T> *b,
narray<T> *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;I<I1;I++)
{
out->data[I] = a->data[I] + b->data[I];
}
return;
}
template<typename T> narray<T> narray<T>::operator+(const narray<T>& other) const
{
narray<T> ret;
narray_size_t I;
int J;
int nthreads;
std::vector<std::thread*> threads;
if(this->length!=other.length)
{
ret.resize(0);
printf("narray<T>::operator+:: error - array sizes %ld and %ld are not the same.\n",(long)this->length,(long)other.length);
}
ret.resize(this->length);
if(this->length<narray_thread_sz)
{
for(I=0;I<this->length;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<nthreads;J++)
{
//printf("debug: launching thread %d\n",J);
threads[J] = new(std::nothrow) std::thread(
&narray_operator_add_tf<T>,
this,
&other,
&ret,
J,nthreads
);
}
for(J=0;J<nthreads;J++)
{
if(threads[J]!=NULL)
{
threads[J]->join();
delete threads[J];
threads[J]= NULL;
}
}
}
return ret;
}
template<typename T> void narray_operator_sub_tf(
const narray<T> *a,
const narray<T> *b,
narray<T> *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;I<I1;I++)
{
out->data[I] = a->data[I] - b->data[I];
}
return;
}
template<typename T> narray<T> narray<T>::operator-(const narray<T>& other) const
{
narray<T> ret;
narray_size_t I;
int J;
int nthreads;
std::vector<std::thread*> threads;
if(this->length!=other.length)
{
ret.resize(0);
printf("narray<T>::operator-:: error - array sizes %ld and %ld are not the same.\n",(long)this->length,(long)other.length);
}
ret.resize(this->length);
if(this->length<narray_thread_sz)
{
for(I=0;I<this->length;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<nthreads;J++)
{
threads[J] = new(std::nothrow) std::thread(
&narray_operator_sub_tf<T>,
this,
&other,
&ret,
J,nthreads
);
}
for(J=0;J<nthreads;J++)
{
if(threads[J]!=NULL)
{
threads[J]->join();
delete threads[J];
threads[J]= NULL;
}
}
}
return ret;
}
template<typename T> void narray_operator_mult_tf(
const narray<T> *a,
const narray<T> *b,
narray<T> *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;I<I1;I++)
{
out->data[I] = a->data[I] * b->data[I];
}
return;
}
template<typename T> narray<T> narray<T>::operator*(const narray<T>& other) const
{
narray<T> ret;
narray_size_t I;
int J;
int nthreads;
std::vector<std::thread*> threads;
if(this->length!=other.length)
{
ret.resize(0);
printf("narray<T>::operator*:: error - array sizes %ld and %ld are not the same.\n",(long)this->length,(long)other.length);
}
ret.resize(this->length);
if(this->length<narray_thread_sz)
{
for(I=0;I<this->length;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<nthreads;J++)
{
threads[J] = new(std::nothrow) std::thread(
&narray_operator_mult_tf<T>,
this,
&other,
&ret,
J,nthreads
);
}
for(J=0;J<nthreads;J++)
{
if(threads[J]!=NULL)
{
threads[J]->join();
delete threads[J];
threads[J]= NULL;
}
}
}
return ret;
}
template<typename T> void narray_operator_div_tf(
const narray<T> *a,
const narray<T> *b,
narray<T> *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;I<I1;I++)
{
out->data[I] = a->data[I] / b->data[I];
}
return;
}
template<typename T> narray<T> narray<T>::operator/(const narray<T>& other) const
{
narray<T> ret;
narray_size_t I;
int J;
int nthreads;
std::vector<std::thread*> threads;
if(this->length!=other.length)
{
ret.resize(0);
printf("narray<T>::operator/:: error - array sizes %ld and %ld are not the same.\n",(long)this->length,(long)other.length);
}
ret.resize(this->length);
if(this->length<narray_thread_sz)
{
for(I=0;I<this->length;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<nthreads;J++)
{
threads[J] = new(std::nothrow) std::thread(
&narray_operator_div_tf<T>,
this,
&other,
&ret,
J,nthreads
);
}
for(J=0;J<nthreads;J++)
{
if(threads[J]!=NULL)
{
threads[J]->join();
delete threads[J];
threads[J]= NULL;
}
}
}
return ret;
}
template<typename T> void narray_operator_add2_tf(
const narray<T> *a,
const T val,
narray<T> *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;I<I1;I++)
{
out->data[I] = a->data[I] + val;
}
return;
}
template<typename T> narray<T> narray<T>::operator+(const T val) const
{
narray<T> ret;
narray_size_t I;
int J;
int nthreads;
std::vector<std::thread*> threads;
ret.resize(this->length);
if(this->length<narray_thread_sz)
{
for(I=0;I<this->length;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<nthreads;J++)
{
threads[J] = new(std::nothrow) std::thread(
&narray_operator_add2_tf<T>,
this,
val,
&ret,
J,nthreads
);
}
for(J=0;J<nthreads;J++)
{
if(threads[J]!=NULL)
{
threads[J]->join();
delete threads[J];
threads[J]= NULL;
}
}
}
return ret;
}
template<typename T> void narray_operator_sub2_tf(
const narray<T> *a,
const T val,
narray<T> *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;I<I1;I++)
{
out->data[I] = a->data[I] - val;
}
return;
}
template<typename T> narray<T> narray<T>::operator-(const T val) const
{
narray<T> ret;
narray_size_t I;
int J;
int nthreads;
std::vector<std::thread*> threads;
ret.resize(this->length);
if(this->length<narray_thread_sz)
{
for(I=0;I<this->length;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<nthreads;J++)
{
threads[J] = new(std::nothrow) std::thread(
&narray_operator_sub2_tf<T>,
this,
val,
&ret,
J,nthreads
);
}
for(J=0;J<nthreads;J++)
{
if(threads[J]!=NULL)
{
threads[J]->join();
delete threads[J];
threads[J]= NULL;
}
}
}
return ret;
}
template<typename T> void narray_operator_mult2_tf(
const narray<T> *a,
const T val,
narray<T> *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;I<I1;I++)
{
out->data[I] = a->data[I] * val;
}
return;
}
template<typename T> narray<T> narray<T>::operator*(const T val) const
{
narray<T> ret;
narray_size_t I;
int J;
int nthreads;
std::vector<std::thread*> threads;
ret.resize(this->length);
if(this->length<narray_thread_sz)
{
for(I=0;I<this->length;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<nthreads;J++)
{
threads[J] = new(std::nothrow) std::thread(
&narray_operator_mult2_tf<T>,
this,
val,
&ret,
J,nthreads
);
}
for(J=0;J<nthreads;J++)
{
if(threads[J]!=NULL)
{
threads[J]->join();
delete threads[J];
threads[J]= NULL;
}
}
}
return ret;
}
template<typename T> void narray_operator_div2_tf(
const narray<T> *a,
const T val,
narray<T> *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;I<I1;I++)
{
out->data[I] = a->data[I] / val;
}
return;
}
template<typename T> narray<T> narray<T>::operator/(const T val) const
{
narray<T> ret;
narray_size_t I;
int J;
int nthreads;
std::vector<std::thread*> threads;
ret.resize(this->length);
if(this->length<narray_thread_sz)
{
for(I=0;I<this->length;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<nthreads;J++)
{
threads[J] = new(std::nothrow) std::thread(
&narray_operator_div2_tf<T>,
this,
val,
&ret,
J,nthreads
);
}
for(J=0;J<nthreads;J++)
{
if(threads[J]!=NULL)
{
threads[J]->join();
delete threads[J];
threads[J]= NULL;
}
}
}
return ret;
}
template<typename T> void narray_operator_negative_tf(
const narray<T> *a,
narray<T> *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;I<I1;I++)
{
out->data[I] = -a->data[I];
}
return;
}
template<class T> narray<T> operator-(const narray<T> &n)
{
narray<T> ret;
narray_size_t I;
int J;
int nthreads;
std::vector<std::thread*> threads;
ret.resize(n.length);
if(n.length<narray_thread_sz)
{
for(I=0;I<n.length;I++) ret.data[I] = -n.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<nthreads;J++)
{
threads[J] = new(std::nothrow) std::thread(
&narray_operator_negative_tf<T>,
&n,
&ret,
J,nthreads
);
}
for(J=0;J<nthreads;J++)
{
if(threads[J]!=NULL)
{
threads[J]->join();
delete threads[J];
threads[J]= NULL;
}
}
}
return ret;
}
template<typename T> void narray_applyfn_tf(
const narray<T> *a,
T (*fnptr)(T),
narray<T> *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;I<I1;I++)
{
out->data[I] = fnptr(a->data[I]);
}
return;
}
template<typename T> narray<T> narray<T>::applyfn(T (*fnptr)(T)) const
{
narray<T> ret;
narray_size_t I;
int J;
int nthreads;
std::vector<std::thread*> threads;
ret.resize(this->length);
if(this->length<narray_thread_sz)
{
for(I=0;I<this->length;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<nthreads;J++)
{
threads[J] = new(std::nothrow) std::thread(
&narray_applyfn_tf<T>,
this,
fnptr,
&ret,
J,nthreads
);
}
for(J=0;J<nthreads;J++)
{
if(threads[J]!=NULL)
{
threads[J]->join();
delete threads[J];
threads[J]= NULL;
}
}
}
return ret;
}
template<typename T> void narray_min_tf(
const narray<T> *in,
narray<T> *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;I<I1;I++)
{
if(in->data[I]<out->data[threadnum])
out->data[threadnum] = in->data[I];
}
return;
}
template<typename T> T narray<T>::min() const
{
T ret = T();
narray<T> minarr;
narray_size_t I;
int J;
int nthreads;
std::vector<std::thread*> threads;
if(this->length<=0)
{
return ret;
}
if(this->length<narray_thread_sz)
{
ret = this->data[0];
for(I=0;I<this->length;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);
minarr.resize(nthreads);
minarr.setall(this->data[0]);
for(J=0;J<nthreads;J++)
{
threads[J] = new(std::nothrow) std::thread(
&narray_min_tf<T>,
this,
&minarr,
J,nthreads
);
}
for(J=0;J<nthreads;J++)
{
if(threads[J]!=NULL)
{
threads[J]->join();
delete threads[J];
threads[J]= NULL;
}
}
ret = this->data[0];
for(J=0;J<nthreads;J++)
{
if(minarr[J]<ret)
{
ret = minarr[J];
}
}
}
return ret;
}
template<typename T> void narray_max_tf(
const narray<T> *in,
narray<T> *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;I<I1;I++)
{
if(in->data[I]>out->data[threadnum])
out->data[threadnum] = in->data[I];
}
return;
}
template<typename T> T narray<T>::max() const
{
T ret = T();
narray<T> maxarr;
narray_size_t I;
int J;
int nthreads;
std::vector<std::thread*> threads;
if(this->length<=0)
{
return ret;
}
if(this->length<narray_thread_sz)
{
ret = this->data[0];
for(I=0;I<this->length;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<nthreads;J++)
{
threads[J] = new(std::nothrow) std::thread(
&narray_max_tf<T>,
this,
&maxarr,
J,nthreads
);
}
for(J=0;J<nthreads;J++)
{
if(threads[J]!=NULL)
{
threads[J]->join();
delete threads[J];
threads[J]= NULL;
}
}
ret = this->data[0];
for(J=0;J<nthreads;J++)
{
if(maxarr[J]>ret)
{
ret = maxarr[J];
}
}
}
return ret;
}
}; //end namespace narray
}; //end namespace ams
#endif
@@ -0,0 +1,44 @@
#ifndef __AMSCPPNARRAY_IMPLSUBARRAY_HPP__
#define __AMSCPPNARRAY_IMPLSUBARRAY_HPP__
namespace ams
{
namespace narray
{
template<typename T> narray<T> narray<T>::subarray(const narray_size_t ind1, const narray_size_t ind2) const
{
int res;
narray<T> ret;
T defval = T();
narray_size_t I,J;
int nl = ind2-ind1;
if(nl<=0)
{
ret.resize(0);
return ret;
}
ret.resize(nl);
for(I=0;I<nl;I++)
{
J = I + ind1;
if(J<0 || J>=this->length)
{
ret.data[I] = defval;
}
else
{
ret.data[I] = this->data[J];
}
}
return ret;
}
};
};
#endif
@@ -0,0 +1,23 @@
#ifndef __AMSCPPNARRAY_LLBOPS_HPP__
#define __AMSCPPNARRAY_LLBOPS_HPP__
//Low level threaded buffer operations
namespace ams
{
namespace narray
{
template<typename T1, typename T2> void buffer_copycast(T1 *bufferto, const T2 *bufferfrom, const narray_size_t N);
template<typename T> void buffer_set(T *buffer, const T val, const narray_size_t ind1, const narray_size_t ind2);
};
};
#include <amscppnarray/amscppnarray_llbopsimpl.hpp>
#endif
@@ -0,0 +1,166 @@
#ifndef __AMSCPPNARRAY_LLBOPSIMPL_HPP__
#define __AMSCPPNARRAY_LLBOPSIMPL_HPP__
namespace ams
{
namespace narray
{
template<typename T1, typename T2> 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<I1;I++)
{
bufferto[I] = (T1) bufferfrom[I];
}
return;
}
template<typename T1, typename T2> void buffer_copycast(T1 *bufferto, const T2 *bufferfrom, const narray_size_t N)
{
narray_size_t I;
int J;
int nthreads;
std::vector<std::thread*> threads;
if(N<=0) return;
if(N<narray_thread_sz)
{
for(I=0;I<N;I++)
{
bufferto[I] = (T1) bufferfrom[I];
}
}
else
{
//threaded operation
nthreads = std::thread::hardware_concurrency();
nthreads = (nthreads<1) ? 1 : nthreads;
nthreads = (nthreads>narray_max_threads) ? narray_max_threads : nthreads;
threads.resize(nthreads);
for(J=0;J<nthreads;J++)
{
threads[J] = new(std::nothrow) std::thread(
&buffer_copycast_tf<T1,T2>,
bufferto,
bufferfrom,
N,
J,nthreads
);
if(threads[J]==NULL)
{
printf("buffer_copy_cast: warning, thread %d failed to launch.\n",J);
}
}
for(J=0;J<nthreads;J++)
{
if(threads[J]!=NULL)
{
threads[J]->join();
delete threads[J];
threads[J]= NULL;
}
}
}
return;
}
template<typename T> 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<I1;I++)
{
bufferto[I] = val;
}
return;
}
template<typename T> 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<std::thread*> threads;
narray_size_t N = ind2-ind1;
if(N<=0) return;
if(N<narray_thread_sz)
{
for(I=ind1;I<ind2;I++)
{
buffer[I] = val;
}
}
else
{
//threaded operation
nthreads = std::thread::hardware_concurrency();
nthreads = (nthreads<1) ? 1 : nthreads;
nthreads = (nthreads>narray_max_threads) ? narray_max_threads : nthreads;
threads.resize(nthreads);
for(J=0;J<nthreads;J++)
{
threads[J] = new(std::nothrow) std::thread(
&buffer_set_tf<T>,
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;J<nthreads;J++)
{
if(threads[J]!=NULL)
{
threads[J]->join();
delete threads[J];
threads[J]= NULL;
}
}
}
return;
}
};
};
#endif
@@ -0,0 +1,29 @@
#ifndef __AMSCPPNARRAY_MATH_HPP__
#define __AMSCPPNARRAY_MATH_HPP__
namespace ams
{
namespace narray
{
template<typename T> T narray_max(const narray<T> &q);
template<typename T> T narray_min(const narray<T> &q);
template<typename T> double narray_mean(const narray<T> &q);
template<typename T> double narray_var(const narray<T> &q);
template<typename T> double narray_stdev(const narray<T> &q);
template<typename T> double narray_L2norm(const narray<T> &q);
template<typename T> T narray_sum(const narray<T> &q);
template<typename T> narray<T> narray_abs(narray<T> &q);
void narray_testmath1();
void narray_testmath2();
void narray_testmath3();
}; //end namespaces
};
#include <amscppnarray/amscppnarray_mathimpl.hpp>
#endif
@@ -0,0 +1,388 @@
#ifndef __AMSCPPNARRAY_MATHIMPL_HPP__
#define __AMSCPPNARRAY_MATHIMPL_HPP__
namespace ams
{
namespace narray
{
template<typename T> T narray_max(const narray<T> &q)
{
return q.max();
}
template<typename T> T narray_min(const narray<T> &q)
{
return q.min();
}
template<typename T> void narray_sum_tf(
const narray<T> *in,
narray<T> *sums,
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;I<I1;I++)
{
sums->data[threadnum] = sums->data[threadnum] + in->data[I];
}
}
template<typename T> T narray_sum(const narray<T> &q)
{
T ret = T();
narray<T> sums;
narray_size_t I;
int J;
int nthreads;
std::vector<std::thread*> threads;
if(q.length<=0)
{
return ret;
}
if(q.length<narray_thread_sz)
{
ret = T();
for(I=0;I<q.length;I++)
{
ret = ret + q.data[I];
}
}
else
{
//threaded operation
nthreads = std::thread::hardware_concurrency();
nthreads = (nthreads<1) ? 1 : nthreads;
nthreads = (nthreads>narray_max_threads) ? narray_max_threads : nthreads;
threads.resize(nthreads);
sums.resize(nthreads);
sums.setall(T());
for(J=0;J<nthreads;J++)
{
threads[J] = new(std::nothrow) std::thread(
&narray_sum_tf<T>,
&q,
&sums,
J,nthreads
);
if(threads[J]==NULL)
{
printf("narray_sum: warning: thread %d failed to initialize.\n",J);
//handle errors
}
}
for(J=0;J<nthreads;J++)
{
if(threads[J]!=NULL)
{
threads[J]->join();
delete threads[J];
threads[J]= NULL;
}
}
ret = T();
for(J=0;J<nthreads;J++)
{
ret = ret + sums.data[J];
}
}
return ret;
}
template<typename T> double narray_mean(const narray<T> &q)
{
double ret = 0.0;
if(q.length>0)
{
T sum = narray_sum(q);
ret = (double)sum/((double)q.length);
}
return ret;
}
template<typename T> void narray_variance_tf(
const narray<T> *in,
narray<double> *sums,
double mean,
int threadnum,
int nthreads
)
{
int I0,I1,Is,I;
double q2;
Is = in->length/nthreads;
I0 = Is*threadnum;
I1 = (threadnum>=(nthreads-1))? in->length: Is*(threadnum+1);
for(I=I0;I<I1;I++)
{
q2 = in->data[I]-mean;
sums->data[threadnum] = sums->data[threadnum] + q2*q2/((double)in->length);
}
}
template<typename T> double narray_var(const narray<T> &q)
{
double ret = 0.0;
narray<double> sums;
narray_size_t I;
int J;
int nthreads;
std::vector<std::thread*> threads;
double mn = ams::narray::narray_mean(q);
double q2;
if(q.length<=0)
{
return ret;
}
if(q.length<narray_thread_sz)
{
ret = 0.0;
for(I=0;I<q.length;I++)
{
q2 = (double)q.data[I]-mn;
ret = ret + q2*q2/((double)q.length);
}
}
else
{
//threaded operation
nthreads = std::thread::hardware_concurrency();
nthreads = (nthreads<1) ? 1 : nthreads;
nthreads = (nthreads>narray_max_threads) ? narray_max_threads : nthreads;
threads.resize(nthreads);
sums.resize(nthreads);
sums.setall(T());
for(J=0;J<nthreads;J++)
{
threads[J] = new(std::nothrow) std::thread(
&narray_variance_tf<T>,
&q,
&sums,
mn,
J,nthreads
);
if(threads[J]==NULL)
{
printf("narray_var: warning: thread %d failed to initialize.\n",J);
//handle errors
}
}
for(J=0;J<nthreads;J++)
{
if(threads[J]!=NULL)
{
threads[J]->join();
delete threads[J];
threads[J]= NULL;
}
}
ret = T();
for(J=0;J<nthreads;J++)
{
ret = ret + sums.data[J];
}
}
return ret;
}
template<typename T> double narray_stdev(const narray<T> &q)
{
double ret = 0.0;
if(q.length>0)
{
ret = narray_var(q);
ret = ::sqrt(ret);
}
return ret;
}
template<typename T> void narray_L2norm_tf(
const narray<T> *in,
narray<double> *sums,
int threadnum,
int nthreads
)
{
int I0,I1,Is,I;
double q2;
Is = in->length/nthreads;
I0 = Is*threadnum;
I1 = (threadnum>=(nthreads-1))? in->length: Is*(threadnum+1);
for(I=I0;I<I1;I++)
{
q2 = in->data[I];
sums->data[threadnum] = sums->data[threadnum] + q2*q2;
}
}
template<typename T> double narray_L2norm(const narray<T> &q)
{
double ret = 0.0;
narray<double> sums;
narray_size_t I;
int J;
int nthreads;
std::vector<std::thread*> threads;
double q2;
if(q.length<=0)
{
return ret;
}
if(q.length<narray_thread_sz)
{
ret = 0.0;
for(I=0;I<q.length;I++)
{
q2 = (double)q.data[I];
ret = ret + q2*q2;
}
}
else
{
//threaded operation
nthreads = std::thread::hardware_concurrency();
nthreads = (nthreads<1) ? 1 : nthreads;
nthreads = (nthreads>narray_max_threads) ? narray_max_threads : nthreads;
threads.resize(nthreads);
sums.resize(nthreads);
sums.setall(T());
for(J=0;J<nthreads;J++)
{
threads[J] = new(std::nothrow) std::thread(
&narray_L2norm_tf<T>,
&q,
&sums,
J,nthreads
);
if(threads[J]==NULL)
{
printf("narray_L2norm: warning: thread %d failed to initialize.\n",J);
//handle errors
}
}
for(J=0;J<nthreads;J++)
{
if(threads[J]!=NULL)
{
threads[J]->join();
delete threads[J];
threads[J]= NULL;
}
}
ret = T();
for(J=0;J<nthreads;J++)
{
ret = ret + sums.data[J];
}
}
ret = ::sqrt(ret);
return ret;
}
template<typename T> void narray_abs_tf(
const narray<T> *in,
narray<T> *out,
int threadnum,
int nthreads
)
{
int I0,I1,Is,I;
double q2;
Is = in->length/nthreads;
I0 = Is*threadnum;
I1 = (threadnum>=(nthreads-1))? in->length: Is*(threadnum+1);
for(I=I0;I<I1;I++)
{
out->data[I] = (in->data[I]<T(0)) ? -in->data[I] : in->data[I];
}
}
template<typename T> narray<T> narray_abs(narray<T> &q)
{
narray<T> ret;
narray_size_t I;
int J;
int nthreads;
std::vector<std::thread*> threads;
if(q.length<=0)
{
ret.resize(0);
return ret;
}
ret.resize(q.length);
if(ret.length!=q.length)
{
printf("narray_abs: error - could not allocate return array.\n");
ret.resize(0);
return ret;
}
if(q.length<narray_thread_sz)
{
for(I=0;I<q.length;I++)
{
ret.data[I] = (q.data[I]<T(0)) ? -q.data[I] : q.data[I];
}
}
else
{
//threaded operation
nthreads = std::thread::hardware_concurrency();
nthreads = (nthreads<1) ? 1 : nthreads;
nthreads = (nthreads>narray_max_threads) ? narray_max_threads : nthreads;
threads.resize(nthreads);
for(J=0;J<nthreads;J++)
{
threads[J] = new(std::nothrow) std::thread(
&narray_abs_tf<T>,
&q,
&ret,
J,nthreads
);
if(threads[J]==NULL)
{
printf("narray_abs: warning: thread %d failed to initialize.\n",J);
//handle errors
}
}
for(J=0;J<nthreads;J++)
{
if(threads[J]!=NULL)
{
threads[J]->join();
delete threads[J];
threads[J]= NULL;
}
}
}
return ret;
}
}; //end namespaces
};
#endif
@@ -0,0 +1,79 @@
#ifndef __AMSCPPNARRAY_RANDOM_HPP__
#define __AMSCPPNARRAY_RANDOM_HPP__
namespace ams
{
namespace narray
{
namespace rand
{
//Random functions for numeric array class
////////////////////////////////////////////////////////////////////////
//Simple Deterministic Psuedorandom Number Generator (32 bit version) //
////////////////////////////////////////////////////////////////////////
typedef int32_t narray_rands_t;
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);
double dpr32_randd(narray_rands_t *seed = &(ams::narray::rand::dpr32_rseed));
float dpr32_randf(narray_rands_t *seed = &(ams::narray::rand::dpr32_rseed));
double dpr32_gaussian(narray_rands_t *seed = &(ams::narray::rand::dpr32_rseed));
float dpr32_gaussianf(narray_rands_t *seed = &(ams::narray::rand::dpr32_rseed));
int dpr32_randint(int low, int high, narray_rands_t *seed = &(ams::narray::rand::dpr32_rseed));
ams::vec2 dpr32_randuvec2(narray_rands_t *seed);
ams::vec3 dpr32_randuvec3(narray_rands_t *seed);
ams::vec4 dpr32_randuvec4(narray_rands_t *seed);
ams::vec2f dpr32_randuvec2f(narray_rands_t *seed);
ams::vec3f dpr32_randuvec3f(narray_rands_t *seed);
ams::vec4f dpr32_randuvec4f(narray_rands_t *seed);
////////////////////////////////////////////
//Numeric array threaded random generators//
////////////////////////////////////////////
narray_rands_t get_rseed();
void set_rseed(narray_rands_t seed);
void set_rseed_withtimer();
narray<double> narray_rand(narray_size_t N,
narray_rands_t *rseed = &(ams::narray::rand::dpr32_rseed));
narray<float> narray_randf(narray_size_t N,
narray_rands_t *rseed = &(ams::narray::rand::dpr32_rseed));
narray<double> narray_randgauss(narray_size_t N,
narray_rands_t *rseed = &(ams::narray::rand::dpr32_rseed));
narray<float> narray_randgaussf(narray_size_t N,
narray_rands_t *rseed = &(ams::narray::rand::dpr32_rseed));
narray<int> narray_randint(narray_size_t N, int low, int highexcl,
narray_rands_t *rseed = &(ams::narray::rand::dpr32_rseed));
narray<vec2> narray_randuvec2(narray_size_t N,
narray_rands_t *rseed = &(ams::narray::rand::dpr32_rseed));
narray<vec3> narray_randuvec3(narray_size_t N,
narray_rands_t *rseed = &(ams::narray::rand::dpr32_rseed));
narray<vec4> narray_randuvec4(narray_size_t N,
narray_rands_t *rseed = &(ams::narray::rand::dpr32_rseed));
narray<vec2f> narray_randuvec2f(narray_size_t N,
narray_rands_t *rseed = &(ams::narray::rand::dpr32_rseed));
narray<vec3f> narray_randuvec3f(narray_size_t N,
narray_rands_t *rseed = &(ams::narray::rand::dpr32_rseed));
narray<vec4f> narray_randuvec4f(narray_size_t N,
narray_rands_t *rseed = &(ams::narray::rand::dpr32_rseed));
void test_random1();
};
};
};
#endif