random updates
This commit is contained in:
@ -13,7 +13,7 @@ namespace cmp
|
||||
|
||||
complex();
|
||||
complex(double _r, double _i);
|
||||
complex(double _r);
|
||||
explicit complex(double _r);
|
||||
complex(const complex &other);
|
||||
complex& operator=(const complex& other);
|
||||
complex& operator=(const double other);
|
||||
|
@ -13,7 +13,7 @@ namespace cmp
|
||||
|
||||
complex64();
|
||||
complex64(float _r, float _i);
|
||||
complex64(float _r);
|
||||
explicit complex64(float _r);
|
||||
complex64(const complex64 &other);
|
||||
complex64& operator=(const complex64& other);
|
||||
complex64& operator=(const float other);
|
||||
|
@ -11,17 +11,36 @@ typedef ::int64_t amsmu_randt2;
|
||||
|
||||
static const amsmu_randt1 dpr32_mod = ( ((amsmu_randt1)1) << ((amsmu_randt1)30) ) - (amsmu_randt1)1;
|
||||
static const amsmu_randt1 dpr32_mult1 = ( (amsmu_randt1) 1201633 );
|
||||
static const amsmu_randt1 dpr32_add1 = ( (amsmu_randt1) 293482 );
|
||||
static const amsmu_randt1 dpr32_add1 = ( (amsmu_randt1) 293487 );
|
||||
|
||||
static const amsmu_randt2 dpr64_mod = ( ((amsmu_randt2)1) << ((amsmu_randt2)62) ) - (amsmu_randt2)1;
|
||||
static const amsmu_randt2 dpr64_mult1 = ( (amsmu_randt2) 1201633L );
|
||||
static const amsmu_randt2 dpr64_add1 = ( (amsmu_randt2) 293482L );
|
||||
static const amsmu_randt2 dpr64_add1 = ( (amsmu_randt2) 293487L );
|
||||
|
||||
extern amsmu_randt1 dpr32_rseed;
|
||||
extern amsmu_randt2 dpr64_rseed;
|
||||
|
||||
amsmu_randt1 dpr32_nextseed(amsmu_randt1 seed);
|
||||
amsmu_randt2 dpr64_nextseed(amsmu_randt2 seed);
|
||||
amsmu_randt1 seed32_next(amsmu_randt1 seed);
|
||||
amsmu_randt2 seed64_next(amsmu_randt2 seed);
|
||||
|
||||
double rand(amsmu_randt1 *seed = &dpr32_rseed);
|
||||
float randf(amsmu_randt1 *seed = &dpr32_rseed);
|
||||
double randgaussian(amsmu_randt1 *seed = &dpr32_rseed);
|
||||
float randgaussianf(amsmu_randt1 *seed = &dpr32_rseed);
|
||||
int randint(int low, int highexcl, amsmu_randt1 *seed = &dpr32_rseed);
|
||||
int64_t randintl(int64_t low, int64_t highexcl, amsmu_randt2 *seed = &dpr64_rseed);
|
||||
|
||||
void seed32_set(amsmu_randt1 _seed);
|
||||
void seed64_set(amsmu_randt2 _seed);
|
||||
void seed_init_timer();
|
||||
|
||||
//Threaded generation of random amsarrays
|
||||
|
||||
amsarray<double> amsarray_rand(amsarray_size_t N, amsmu_randt1 *seed = &dpr32_rseed);
|
||||
amsarray<float> amsarray_randf(amsarray_size_t N, amsmu_randt1 *seed = &dpr32_rseed);
|
||||
|
||||
amsarray<int> amsarray_randint(amsarray_size_t N, int low, int highexcl, amsmu_randt1 *seed = &dpr32_rseed);
|
||||
amsarray<int64_t> amsarray_randintl(amsarray_size_t N, int64_t low, int64_t highexcl, amsmu_randt2 *seed = &dpr64_rseed);
|
||||
|
||||
|
||||
|
||||
|
@ -8,9 +8,11 @@ namespace amsmathutil25
|
||||
|
||||
void test_amsarray1();
|
||||
void test_amsarray2();
|
||||
|
||||
void test_select();
|
||||
void test_amsarray_sort();
|
||||
void test_amsarray_select();
|
||||
void test_amsarray_sort1();
|
||||
|
||||
|
||||
|
||||
}; //end namespace amsmathutil25
|
||||
}; //end namespace ams
|
||||
|
@ -7,6 +7,8 @@ namespace ams
|
||||
static const int amsarray_success = amsmathutil25_success;
|
||||
static const int amsarray_failure = amsmathutil25_failure;
|
||||
|
||||
static const int amsarray_sortthreadpsize = 5000;
|
||||
|
||||
template<typename T> class amsarray
|
||||
{
|
||||
public:
|
||||
@ -109,6 +111,9 @@ namespace ams
|
||||
// {0, 1, 2, .... N}
|
||||
amsarray<amsarray_size_t> permutation_identity(amsarray_size_t _length);
|
||||
|
||||
void _debug_amsarray_print(amsarray<double> *array, bool newline=1,int printstyle=0);
|
||||
void _debug_amsarray_print(amsarray<long> *array, bool newline=1,int printstyle=0);
|
||||
|
||||
|
||||
}; //end namespace ams
|
||||
|
||||
|
@ -41,9 +41,10 @@ template<typename T> int amsarray_quicksort_round(
|
||||
if(v2<v1)
|
||||
{
|
||||
//swap permutation indices
|
||||
tmp = permarray->data[range.a];
|
||||
permarray->data[range.a] = permarray->data[range.b-1];
|
||||
permarray->data[range.b-1] = tmp;
|
||||
amsarray_permutation_swap(permarray,range.a,range.b-1);
|
||||
// tmp = permarray->data[range.a];
|
||||
// permarray->data[range.a] = permarray->data[range.b-1];
|
||||
// permarray->data[range.b-1] = tmp;
|
||||
}
|
||||
//there is no more work to be done within this range
|
||||
*leftrange = ams::pair<amsarray_size_t,amsarray_size_t>(-1,-1);
|
||||
@ -63,18 +64,28 @@ template<typename T> int amsarray_quicksort_round(
|
||||
amsarray_permutation_swap(permarray,P,range.b-1);
|
||||
P = range.b-1;
|
||||
|
||||
// amsarray_permutation_swap(permarray,P,range.a);
|
||||
// P = range.a;
|
||||
|
||||
|
||||
J = range.a;
|
||||
for(I=range.a;I<range.b-1;I++)
|
||||
{
|
||||
|
||||
//printf("debug: I=%ld, J=%ld, P=%ld, a=%1.3f, b=%1.3f\n",I,J,P,
|
||||
// (double)array->data[permarray->data[I]],(double)array->data[permarray->data[P]]);
|
||||
|
||||
if(array->data[permarray->data[I]]<array->data[permarray->data[P]])
|
||||
{
|
||||
if(J!=I)
|
||||
{
|
||||
//printf("debug: swap\n");
|
||||
amsarray_permutation_swap(permarray,I,J);
|
||||
J++;
|
||||
}
|
||||
else
|
||||
{
|
||||
//printf("debug: skip\n");
|
||||
J++;
|
||||
}
|
||||
}
|
||||
@ -82,7 +93,7 @@ template<typename T> int amsarray_quicksort_round(
|
||||
|
||||
if(array->data[permarray->data[J]]<array->data[permarray->data[P]])
|
||||
{
|
||||
J++;
|
||||
J++;
|
||||
amsarray_permutation_swap(permarray,P,J);
|
||||
|
||||
}
|
||||
@ -119,6 +130,42 @@ template<typename T> int amsarray_quicksort_round(
|
||||
return ret;
|
||||
}
|
||||
|
||||
template<typename T> int amsarray_quicksort_subrange(
|
||||
amsarray<T> *array, //size N - array to sort
|
||||
amsarray<amsarray_size_t> *permarray, //size N - permutation of sorting
|
||||
ams::pair<amsarray_size_t,amsarray_size_t> _range
|
||||
)
|
||||
{
|
||||
int ret = amsarray_success;
|
||||
int res;
|
||||
|
||||
amsarray<ams::pair<amsarray_size_t,amsarray_size_t>> ranges;
|
||||
amsarray_size_t rangeptr = 0;
|
||||
ams::pair<amsarray_size_t,amsarray_size_t> range,rangeleft,rangeright;
|
||||
|
||||
ranges.append(_range);
|
||||
rangeptr = 0;
|
||||
while(rangeptr<ranges.length)
|
||||
{
|
||||
//printf("debug2:"); _debug_amsarray_print(permarray);
|
||||
range = ranges[rangeptr];
|
||||
rangeptr++;
|
||||
//printf("debug3: range=(%d,%d)\n",(int)range.a,(int)range.b);
|
||||
amsarray_quicksort_round(array,permarray,range,&rangeleft,&rangeright);
|
||||
if(rangeleft.a>=0 && rangeleft.b>rangeleft.a)
|
||||
{
|
||||
ranges.append(rangeleft);
|
||||
}
|
||||
if(rangeright.a>=0 && rangeright.b>rangeright.a)
|
||||
{
|
||||
ranges.append(rangeright);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
template<typename T> int amsarray_quicksort_unthreaded(
|
||||
amsarray<T> *array, //size N - array to sort
|
||||
amsarray<amsarray_size_t> *permarray //size N - permutation of sorting
|
||||
@ -145,8 +192,10 @@ template<typename T> int amsarray_quicksort_unthreaded(
|
||||
rangeptr = 0;
|
||||
while(rangeptr<ranges.length)
|
||||
{
|
||||
//printf("debug2:"); _debug_amsarray_print(permarray);
|
||||
range = ranges[rangeptr];
|
||||
rangeptr++;
|
||||
//printf("debug3: range=(%d,%d)\n",(int)range.a,(int)range.b);
|
||||
amsarray_quicksort_round(array,permarray,range,&rangeleft,&rangeright);
|
||||
if(rangeleft.a>=0 && rangeleft.b>rangeleft.a)
|
||||
{
|
||||
@ -173,31 +222,48 @@ template<typename T> void amsarray_quicksort_tf(
|
||||
|
||||
ams::pair<amsarray_size_t,amsarray_size_t> rangeleft,rangeright;
|
||||
|
||||
res = amsarray_quicksort_round(
|
||||
array,
|
||||
permarray,
|
||||
range,
|
||||
&rangeleft,
|
||||
&rangeright
|
||||
);
|
||||
|
||||
{ //scope wrapper for std::lock_guard
|
||||
std::lock_guard<std::mutex> lock(*threadlock);
|
||||
//critical section
|
||||
if(rangeleft.a>=0 && rangeleft.b>rangeleft.a)
|
||||
{
|
||||
ranges->append(rangeleft);
|
||||
}
|
||||
if(rangeright.a>=0 && rangeright.b>rangeright.a)
|
||||
{
|
||||
ranges->append(rangeright);
|
||||
}
|
||||
if(range.b-range.a < amsarray_sortthreadpsize)
|
||||
{
|
||||
res = amsarray_quicksort_subrange(
|
||||
array,
|
||||
permarray,
|
||||
range
|
||||
);
|
||||
//there should be no work to be done after this
|
||||
}
|
||||
else
|
||||
{
|
||||
//range is too big, quicksort the pivot and supply subranges
|
||||
res = amsarray_quicksort_round(
|
||||
array,
|
||||
permarray,
|
||||
range,
|
||||
&rangeleft,
|
||||
&rangeright
|
||||
);
|
||||
|
||||
{ //scope wrapper for std::lock_guard
|
||||
std::lock_guard<std::mutex> lock(*threadlock);
|
||||
//critical section
|
||||
if(rangeleft.a>=0 && rangeleft.b>rangeleft.a)
|
||||
{
|
||||
ranges->append(rangeleft);
|
||||
}
|
||||
if(rangeright.a>=0 && rangeright.b>rangeright.a)
|
||||
{
|
||||
ranges->append(rangeright);
|
||||
}
|
||||
}
|
||||
//end critical section (end of function)
|
||||
}
|
||||
//end critical section (end of function)
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
//TODO - if the range falls below a specified size, I want to be able to run through
|
||||
// quicksorting the entire range within a thread before returning
|
||||
|
||||
|
||||
template<typename T> int amsarray_quicksort_threaded(
|
||||
amsarray<T> *array, //size N - array to sort
|
||||
amsarray<amsarray_size_t> *permarray //size N - permutation of sorting
|
||||
@ -240,9 +306,13 @@ template<typename T> int amsarray_quicksort_threaded(
|
||||
nthreads = ranges.length-rangeptr;
|
||||
nthreads = (nthreads>maxthreads) ? maxthreads : nthreads;
|
||||
|
||||
//printf("debug: %d %d %ld %ld\n",nthreads,maxthreads,rangeptr,ranges.length);
|
||||
for(I=0;I<nthreads;I++)
|
||||
{
|
||||
range = ranges[rangeptr+I];
|
||||
threadlock.lock();
|
||||
range = ranges[rangeptr]; rangeptr++;
|
||||
threadlock.unlock();
|
||||
//printf("debug: thread %ld exec with range(%ld,%ld), rptr=%ld rlen=%ld\n",I,range.a,range.b,rangeptr,ranges.length);
|
||||
threads[I] = new(std::nothrow) std::thread(
|
||||
amsarray_quicksort_tf<T>,
|
||||
array,permarray,
|
||||
@ -259,6 +329,7 @@ template<typename T> int amsarray_quicksort_threaded(
|
||||
ret = amsarray_failure;
|
||||
}
|
||||
}
|
||||
//printf("debug3\n");
|
||||
for(I=0;I<nthreads;I++)
|
||||
{
|
||||
if(threads[I]!=NULL)
|
||||
@ -267,7 +338,7 @@ template<typename T> int amsarray_quicksort_threaded(
|
||||
delete threads[I];
|
||||
threads[I] = NULL;
|
||||
}
|
||||
rangeptr++;
|
||||
//rangeptr++;
|
||||
}
|
||||
}
|
||||
|
||||
@ -292,7 +363,7 @@ template<typename T> int amsarray_quicksort(
|
||||
}
|
||||
}
|
||||
|
||||
if(array->length<amsmathutil25_threadpsz)
|
||||
if(array->length<amsarray_sortthreadpsize)
|
||||
{
|
||||
//perform unthreaded quicksort
|
||||
ret = amsarray_quicksort_unthreaded(
|
||||
|
@ -28,6 +28,9 @@ public:
|
||||
triple(const T1 &_a, const T2& _b, const T3& _c) {a = _a; b = _b; c = _c;}
|
||||
};
|
||||
|
||||
//returns time in msec
|
||||
double time_msec();
|
||||
|
||||
}; //end namespace ams
|
||||
|
||||
#include <amsmathutil25/util/amsmathutil25_utilimpl.hpp>
|
||||
|
Reference in New Issue
Block a user