update
parent
8a9f679a6c
commit
0866d42781
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -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,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,12 @@
|
|||||||
|
#ifndef __AMSCPPNARRAY_RANDOM_HPP__
|
||||||
|
#define __AMSCPPNARRAY_RANDOM_HPP__
|
||||||
|
|
||||||
|
namespace ams
|
||||||
|
{
|
||||||
|
namespace narray
|
||||||
|
{
|
||||||
|
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
@ -0,0 +1,173 @@
|
|||||||
|
#include <amscppnarray/amscppnarray.hpp>
|
||||||
|
|
||||||
|
namespace ams
|
||||||
|
{
|
||||||
|
namespace narray
|
||||||
|
{
|
||||||
|
|
||||||
|
void narray_linspacef_tf(
|
||||||
|
narray<float> *in,
|
||||||
|
const float low, const float high, const narray_size_t N,
|
||||||
|
int threadnum, int nthreads
|
||||||
|
)
|
||||||
|
{
|
||||||
|
int I0,I1,Is,I;
|
||||||
|
float g;
|
||||||
|
|
||||||
|
Is = in->length/nthreads;
|
||||||
|
I0 = Is*threadnum;
|
||||||
|
I1 = (threadnum>=(nthreads-1))? in->length: Is*(threadnum+1);
|
||||||
|
for(I=I0;I<I1;I++)
|
||||||
|
{
|
||||||
|
g = ((float)I)/((float)(N-1));
|
||||||
|
in->data[I] = low*(1.0f-g) + high*g;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
narray<float> linspacef(const float low, const float high, narray_size_t N)
|
||||||
|
{
|
||||||
|
narray<float> ret;
|
||||||
|
narray_size_t I;
|
||||||
|
int J;
|
||||||
|
int nthreads;
|
||||||
|
std::vector<std::thread*> threads;
|
||||||
|
float g;
|
||||||
|
|
||||||
|
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++)
|
||||||
|
{
|
||||||
|
g = ((float)I)/((float)(N-1));
|
||||||
|
ret.data[I] = low*(1.0f-g) + high*g;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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_linspacef_tf,
|
||||||
|
&ret,
|
||||||
|
low,high,N,
|
||||||
|
J,nthreads
|
||||||
|
);
|
||||||
|
}
|
||||||
|
for(J=0;J<nthreads;J++)
|
||||||
|
{
|
||||||
|
if(threads[J]!=NULL)
|
||||||
|
{
|
||||||
|
threads[J]->join();
|
||||||
|
delete threads[J];
|
||||||
|
threads[J]= NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
void narray_linspace_tf(
|
||||||
|
narray<double> *in,
|
||||||
|
const double low, const double high, const narray_size_t N,
|
||||||
|
int threadnum, int nthreads
|
||||||
|
)
|
||||||
|
{
|
||||||
|
int I0,I1,Is,I;
|
||||||
|
double g;
|
||||||
|
|
||||||
|
Is = in->length/nthreads;
|
||||||
|
I0 = Is*threadnum;
|
||||||
|
I1 = (threadnum>=(nthreads-1))? in->length: Is*(threadnum+1);
|
||||||
|
for(I=I0;I<I1;I++)
|
||||||
|
{
|
||||||
|
g = ((double)I)/((double)(N-1));
|
||||||
|
in->data[I] = low*(1.0-g) + high*g;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
narray<double> linspace(const double low, const double high, narray_size_t N)
|
||||||
|
{
|
||||||
|
narray<double> ret;
|
||||||
|
narray_size_t I;
|
||||||
|
int J;
|
||||||
|
int nthreads;
|
||||||
|
std::vector<std::thread*> threads;
|
||||||
|
double g;
|
||||||
|
|
||||||
|
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++)
|
||||||
|
{
|
||||||
|
g = ((double)I)/((double)(N-1));
|
||||||
|
ret.data[I] = low*(1.0-g) + high*g;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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_linspace_tf,
|
||||||
|
&ret,
|
||||||
|
low,high,N,
|
||||||
|
J,nthreads
|
||||||
|
);
|
||||||
|
}
|
||||||
|
for(J=0;J<nthreads;J++)
|
||||||
|
{
|
||||||
|
if(threads[J]!=NULL)
|
||||||
|
{
|
||||||
|
threads[J]->join();
|
||||||
|
delete threads[J];
|
||||||
|
threads[J]= NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}; //end namespaces
|
||||||
|
};
|
@ -0,0 +1,11 @@
|
|||||||
|
#include <amscppnarray/amscppnarray.hpp>
|
||||||
|
|
||||||
|
namespace ams
|
||||||
|
{
|
||||||
|
namespace narray
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}; //end namespaces
|
||||||
|
};
|
Loading…
Reference in New Issue