host random buffer fillers

This commit is contained in:
2026-04-14 21:20:49 -04:00
parent b0121a2f83
commit 361ee4c6da
42 changed files with 540 additions and 57 deletions

View File

@ -7,7 +7,12 @@
#include <math.h>
#include <stdint.h>
#include <time.h>
//C++ standard library headers
#include <new>
#include <thread>
#include <functional>
#include <mutex>
#include <cuda_runtime_api.h> //where all the cuda functions live
#include <cuda_runtime.h>
@ -41,12 +46,18 @@ namespace amscuda
//default numthreads to execute on cpu
AMSCU_CONST static const int amscu_defcputhreads = 8;
AMSCU_CONST static const int amscu_success = 1;
AMSCU_CONST static const int amscu_meh = 0;
AMSCU_CONST static const int amscu_failure = -1;
}; //end namespace amscuda
//Components
#include <amsculib3/amscu_cudafunctions.hpp>
#include <amsculib3/math/amscumath.hpp>
#include <amsculib3/geom/amscugeom.hpp>
#include <amsculib3/util/amscu_util.hpp>
#include <amsculib3/amscuarray.hpp>
#include <amsculib3/amscuda_binarrrw.hpp>

View File

@ -34,18 +34,18 @@ __host__ __device__ float randnf(randstate_t *state = NULL);
__host__ __device__ double randn(randstate_t *state = NULL);
//Operations to fill a host buffer with random values
__host__ int hbuff_randf(float *hbuffer, int64_t size, randstate_t *state = NULL);
__host__ int hbuff_rand(double *hbuffer, int64_t size, randstate_t *state = NULL);
__host__ int hbuff_randnf(float *hbuffer, int64_t size, randstate_t *state = NULL);
__host__ int hbuff_randn(double *hbuffer, int64_t size, randstate_t *state = NULL);
__host__ int hbuff_randint(int *hbuffer, int64_t size, int low, int high, randstate_t *state = NULL);
__host__ int hbuff_randf(float *hbuffer, int size, randstate_t *state = NULL);
__host__ int hbuff_rand(double *hbuffer, int size, randstate_t *state = NULL);
__host__ int hbuff_randnf(float *hbuffer, int size, randstate_t *state = NULL);
__host__ int hbuff_randn(double *hbuffer, int size, randstate_t *state = NULL);
__host__ int hbuff_randint(int *hbuffer, int size, int low, int high, randstate_t *state = NULL);
//Operations to fill a device buffer with random values
__host__ int dbuff_randf(float *hbuffer, int64_t size, randstate_t *state = NULL);
__host__ int dbuff_rand(double *hbuffer, int64_t size, randstate_t *state = NULL);
__host__ int dbuff_randnf(float *hbuffer, int64_t size, randstate_t *state = NULL);
__host__ int dbuff_randn(double *hbuffer, int64_t size, randstate_t *state = NULL);
__host__ int dbuff_randint(int *hbuffer, int64_t size, int low, int high, randstate_t *state = NULL);
__host__ int dbuff_randf(float *dbuffer, int size, randstate_t *state = NULL);
__host__ int dbuff_rand(double *dbuffer, int size, randstate_t *state = NULL);
__host__ int dbuff_randnf(float *dbuffer, int size, randstate_t *state = NULL);
__host__ int dbuff_randn(double *dbuffer, int size, randstate_t *state = NULL);
__host__ int dbuff_randint(int *dbuffer, int size, int low, int high, randstate_t *state = NULL);
//Tests

View File

@ -0,0 +1,22 @@
#ifndef __AMSCU_CPUTHREADING_HPP__
#define __AMSCU_CPUTHREADING_HPP__
namespace amscuda
{
namespace util
{
int amscpu_cputhreading_threadplan(int probsize);
//A template function that takes as input a function pointer and a series of arguments
//The function is executed with fptr(threadnum, nthreads, otherargs...) with a dynamic number of threads
//psize must be supplied, which will call amscpu_cputhreading_threadplan to determine number of threads to use for execution
template<typename callable, typename ... argst> int threaded_execute(callable &&fptr, int64_t psize, argst&&... args);
};
};
#include <amsculib3/util/amscu_cputhreading_impl.hpp>
#endif

View File

@ -0,0 +1,73 @@
#ifndef __AMSCU_CPUTHREADING_IMPL_HPP__
#define __AMSCU_CPUTHREADING_IMPL_HPP__
namespace amscuda
{
namespace util
{
template<typename callable, typename ... argst> int threaded_execute(callable &&fptr, int64_t psize, argst&&... args)
{
int ret = amscu_success;
int I;
std::vector<std::thread*> threads;
int nthreads = amscpu_cputhreading_threadplan(psize);
if(nthreads<=1)
{
nthreads = 1;
I = 0;
// std::invoke(
// std::forward<callable>(fptr),
// I,
// nthreads,
// std::forward<argst>(args)...
// );
//std::invoke is a C++17 feature, and mingw8 complains even so.
// Can I get away with just calling the functions?
fptr(I,nthreads,std::forward<argst>(args)...);
}
else
{
threads.resize(nthreads);
for(I=0;I<nthreads;I++) threads[I] = NULL;
for(I=0;I<nthreads;I++)
{
threads[I] = new(std::nothrow) std::thread
(
std::forward<callable>(fptr),
I,
nthreads,
std::forward<argst>(args)...
);
}
for(I=0;I<nthreads;I++)
{
if(threads[I]==NULL)
{ //null thread creation failure check
//printf("debug check!\n");
ret = amscu_failure;
}
}
for(I=0;I<nthreads;I++)
{
if(threads[I]!=NULL)
{
threads[I]->join();
delete threads[I];
threads[I] = NULL;
}
}
}
return ret;
}
};
};
#endif

View File

@ -0,0 +1,15 @@
#ifndef __AMSCU_UTIL_CUH__
#define __AMSCU_UTIL_CUH__
namespace amscuda
{
namespace util
{
};
};
#include <amsculib3/util/amscu_cputhreading.hpp>
#endif