cpp : Added documentation

This commit is contained in:
saundersp
2024-04-28 22:11:33 +02:00
parent f7ac38b93a
commit c71b04f00d
16 changed files with 797 additions and 295 deletions

View File

@ -35,10 +35,10 @@ namespace np {
#endif
__host__ __device__
// #if __DEBUG
// print("Shape created (default)");
// #endif
Shape(void) noexcept {
#if __DEBUG
printf("Shape created (default)\n");
#endif
}
__host__ __device__
@ -52,9 +52,9 @@ namespace np {
__host__ __device__
Shape(const std::initializer_list<size_t>& dims) noexcept : length(dims.size()), data(new size_t[dims.size()]), refcount(new size_t(1)) {
// #if __DEBUG
// print("Shape created (initializer)");
// #endif
#if __DEBUG
printf("Shape created (initializer)\n");
#endif
const size_t* const begin = dims.begin();
for(size_t i = 0; i < length; ++i){
data[i] = begin[i];
@ -98,9 +98,9 @@ namespace np {
__host__ __device__
Shape(Shape&& shape) noexcept {
// #if __DEBUG
// print("Shape created (move));
// #endif
#if __DEBUG
printf("Shape created (move)\n");
#endif
if (data != nullptr && data != shape.data){
#if __DEBUG
printf("Former shape deleted (move)\n");
@ -129,27 +129,27 @@ namespace np {
__host__ __device__
~Shape(void) noexcept {
if(refcount == nullptr){
// #if __DEBUG
// print("Shape refcount freed more than once");
// #endif
#if __DEBUG
printf("Shape refcount freed more than once\n");
#endif
return;
}
--(*refcount);
// #if __DEBUG
// printf("Shape destructed : %lu\n", *refcount);
// #endif
#if __DEBUG
printf("Shape destructed : %lu\n", *refcount);
#endif
if(*refcount == 0){
if (data != nullptr){
delete[] data;
data = nullptr;
// #if __DEBUG
// print("Shape freeing ...");
// #endif
#if __DEBUG
printf("Shape freeing ...\n");
#endif
}
//#if __DEBUG
#if __DEBUG
else
printf("Shape freed more than once : %lu\n", *refcount);
//#endif
#endif
delete refcount;
refcount = nullptr;
#if __DEBUG
@ -191,9 +191,9 @@ namespace np {
__host__ __device__
Shape& operator=(Shape&& shape) noexcept {
// #if __DEBUG
// print("Shape created (assign move)");
// #endif
#if __DEBUG
printf("Shape created (assign move)\n");
#endif
if (data != nullptr && data != shape.data){
#if __DEBUG
printf("Former shape deleted (assign move)\n");
@ -210,6 +210,8 @@ namespace np {
data = shape.data;
refcount = shape.refcount;
#if __DEBUG
if (refcount == nullptr)
printf("Assigned copy shape has null refcount\n");
total = shape.total;
shape.total = 1;
#endif
@ -261,37 +263,38 @@ namespace np {
size_t* refcount = nullptr;
__host__ __device__
// #if __DEBUG
// print("Array created (default)");
Array(void) noexcept {
#if __DEBUG
printf("Array created (default)\n");
#endif
}
__host__ __device__
// #if __DEBUG
// print("Array created (raw, copy shape)");
// #endif
Array(const Shape& shape, T* const data) noexcept : shape(shape), data(data), refcount(new size_t(1)) {
#if __DEBUG
printf("Array created (raw, copy shape)\n");
#endif
}
__host__ __device__
Array(const Shape& shape) noexcept : shape(shape), data(new T[np::prod(shape)]), refcount(new size_t(1)) {
// #if __DEBUG
// print("Array created (raw empty, copy shape)");
// #endif
#if __DEBUG
printf("Array created (raw empty, copy shape)\n");
#endif
}
__host__ __device__
// #if __DEBUG
// print("Array created (raw, move shape)");
// #endif
Array(Shape&& shape, T* const data) noexcept : shape(shape), data(data), refcount(new size_t(1)) {
#if __DEBUG
printf("Array created (raw, move shape)\n");
#endif
}
__host__ __device__
// #if __DEBUG
// print("Array created (raw empty, move shape)");
// #endif
Array(Shape&& shape) noexcept : shape(shape), data(new T[np::prod(shape)]), refcount(new size_t(1)) {
#if __DEBUG
printf("Array created (raw empty, move shape)\n");
#endif
}
__host__ __device__
@ -323,10 +326,10 @@ namespace np {
}
__host__ __device__
// #if __DEBUG
// print("Array created (move)");
// #endif
Array(Array&& array) noexcept : shape(std::move(array.shape)) {
#if __DEBUG
printf("Array created (move)\n");
#endif
if (data != nullptr && data != array.data){
#if __DEBUG
printf("Former array deleted (move)\n");
@ -349,22 +352,22 @@ namespace np {
__host__ __device__
~Array(void) noexcept {
if(refcount == nullptr){
// #if __DEBUG
// print("Array refcount freed more than once");
// #endif
#if __DEBUG
printf("Array refcount freed more than once\n");
#endif
return;
}
--(*refcount);
// #if __DEBUG
// printf("Array destructed : %lu\n", *refcount);
// #endif
#if __DEBUG
printf("Array destructed : %lu\n", *refcount);
#endif
if(*refcount == 0){
if (data != nullptr){
delete[] data;
data = nullptr;
// #if __DEBUG
// print("Array freeing ...");
// #endif
#if __DEBUG
printf("Array freeing ...\n");
#endif
}
#if __DEBUG
else
@ -400,16 +403,16 @@ namespace np {
(*refcount)++;
#if __DEBUG
else
#endif
printf("Assigned array has null refcount\n");
#endif
return *this;
}
__host__ __device__
Array& operator=(Array&& array) noexcept {
// #if __DEBUG
// print("Array created (assign move)");
// #endif
#if __DEBUG
printf("Array created (assign move)\n");
#endif
if (data != nullptr && data != array.data){
#if __DEBUG
printf("Former array deleted (assign move)\n");
@ -786,48 +789,6 @@ static size_t as_partition(const T* const a, uint16_t* const indices, const size
return i;
}
template<typename T>
void argsort(const T* const a, uint16_t* const indices, const size_t& l, const size_t& h) noexcept {
const size_t total = h - l + 1;
size_t* const stack = new size_t[total]{l, h};
size_t top = 1, low = l, high = h;
while (top <= total) {
high = stack[top--];
low = stack[top--];
if(low >= high)
break;
const size_t p = as_partition(a, indices, low, high);
if (p - 1 > low && p - 1 < total) {
stack[++top] = low;
stack[++top] = p - 1;
}
if (p + 1 < high) {
stack[++top] = p + 1;
stack[++top] = high;
}
}
delete[] stack;
}
template<typename T>
np::Array<uint16_t> argsort(const np::Array<T>& other, const size_t& l, const size_t& h) noexcept {
np::Array<uint16_t> indices = np::empty(other.shape);
map(indices, [](const size_t& i, const uint16_t&) -> uint16_t { return i; });
argsort(other, indices, l, h);
return indices;
}
template<typename T>
np::Array<uint16_t> argsort(const np::Array<T>* const other, const size_t& length) noexcept {
return argsort(other, 0, length - 1);
}
std::array<np::Array<uint8_t>, 4> load_datasets(void);
void print_error_file(const char* const) noexcept;
@ -838,10 +799,10 @@ void save(const np::Array<T>& d, const char* const filename) {
print_error_file(filename);
throw;
}
assert(d.shape.refcount != 0);//, "Refcount shape is zero !!");
assert(d.shape.refcount != 0);
fwrite(&d.shape.length, sizeof(size_t), 1, output);
fwrite(d.shape.data, sizeof(size_t), d.shape.length, output);
assert(d.refcount != 0);//, "Refcount array is zero !!");
assert(d.refcount != 0);
fwrite(d.data, sizeof(T), np::prod(d.shape), output);
fclose(output);
}