Moved DEBUG option to config files

This commit is contained in:
saundersp
2023-07-14 23:57:58 +02:00
parent e6194ac485
commit 399024da7a
12 changed files with 280 additions and 268 deletions

View File

@@ -5,10 +5,8 @@
#include <cassert>
#include <functional>
#include <memory>
#include "config.hpp"
#define DATA_DIR "../data"
#define OUT_DIR "./out"
#define MODEL_DIR "./models"
#define BUFFER_SIZE 256
#define STRING_INT_SIZE 8 // Length of a number in log10 (including '-')
#define S(N) std::string(N, '-').c_str()
@@ -42,20 +40,20 @@ namespace np {
size_t length = 0;
size_t* data = nullptr;
size_t* refcount = nullptr;
#ifdef __DEBUG
#if __DEBUG
size_t total = 1;
#endif
__host__ __device__
Shape() noexcept {
// #ifdef __DEBUG
// #if __DEBUG
// print("Shape created (default)");
// #endif
}
__host__ __device__
Shape(const size_t& length, size_t* data) noexcept : length(length), data(data), refcount(new size_t(1)) {
#ifdef __DEBUG
#if __DEBUG
//print("Shape created (raw)");
for(size_t i = 0; i < length; ++i)
total *= data[i];
@@ -64,13 +62,13 @@ 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)) {
// #ifdef __DEBUG
// #if __DEBUG
// print("Shape created (initializer)");
// #endif
const auto* begin = dims.begin();
for(size_t i = 0; i < length; ++i){
data[i] = begin[i];
#ifdef __DEBUG
#if __DEBUG
total *= data[i];
#endif
}
@@ -78,17 +76,17 @@ namespace np {
__host__ __device__
Shape(const Shape& shape) noexcept {
#ifdef __DEBUG
#if __DEBUG
print("Shape created (copy)");
#endif
if (data != nullptr && data != shape.data){
#ifdef __DEBUG
#if __DEBUG
print("Former shape deleted (copy)");
#endif
delete[] data;
}
if (refcount != nullptr && refcount != shape.refcount){
#ifdef __DEBUG
#if __DEBUG
print("Former shape refcount freed (copy)");
#endif
delete refcount;
@@ -104,7 +102,7 @@ namespace np {
refcount = shape.refcount;
if (refcount != nullptr)
(*refcount)++;
#ifdef __DEBUG
#if __DEBUG
else
print("Moved shape has null refcount");
total = shape.total;
@@ -113,17 +111,17 @@ namespace np {
__host__ __device__
Shape(Shape&& shape) noexcept {
// #ifdef __DEBUG
// #if __DEBUG
// print("Shape created (move));
// #endif
if (data != nullptr && data != shape.data){
#ifdef __DEBUG
#if __DEBUG
print("Former shape deleted (move)");
#endif
delete[] data;
}
if (refcount != nullptr && refcount != shape.refcount){
#ifdef __DEBUG
#if __DEBUG
print("Former shape refcount freed (move)");
#endif
delete refcount;
@@ -135,7 +133,7 @@ namespace np {
shape.length = 0;
shape.data = nullptr;
shape.refcount = nullptr;
#ifdef __DEBUG
#if __DEBUG
total = shape.total;
shape.total = 1;
#endif
@@ -144,30 +142,30 @@ namespace np {
__host__ __device__
~Shape() noexcept {
if(refcount == nullptr){
// #ifdef __DEBUG
// #if __DEBUG
// print("Shape refcount freed more than once");
// #endif
return;
}
--(*refcount);
// #ifdef __DEBUG
// #if __DEBUG
// printf("Shape destructed : %lu\n", *refcount);
// #endif
if(*refcount == 0){
if (data != nullptr){
delete[] data;
data = nullptr;
// #ifdef __DEBUG
// #if __DEBUG
// print("Shape freeing ...");
// #endif
}
//#ifdef __DEBUG
//#if __DEBUG
else
printf("Shape freed more than once : %lu\n", *refcount);
//#endif
delete refcount;
refcount = nullptr;
#ifdef __DEBUG
#if __DEBUG
total = 1;
#endif
}
@@ -175,17 +173,17 @@ namespace np {
__host__ __device__
Shape& operator=(const Shape& shape) noexcept {
#ifdef __DEBUG
#if __DEBUG
print("Shape created (assign copy)");
#endif
if (data != nullptr && data != shape.data){
#ifdef __DEBUG
#if __DEBUG
print("Former shape deleted (assign copy)");
#endif
delete[] data;
}
if (refcount != nullptr && refcount != shape.refcount){
#ifdef __DEBUG
#if __DEBUG
print("Former shape refcount freed (assign copy)");
#endif
delete refcount;
@@ -201,7 +199,7 @@ namespace np {
refcount = shape.refcount;
if (refcount != nullptr)
(*refcount)++;
#ifdef __DEBUG
#if __DEBUG
else
printf("Assigned copy shape has null refcount");
total = shape.total;
@@ -211,17 +209,17 @@ namespace np {
__host__ __device__
Shape& operator=(Shape&& shape) noexcept {
// #ifdef __DEBUG
// #if __DEBUG
// print("Shape created (assign move)");
// #endif
if (data != nullptr && data != shape.data){
#ifdef __DEBUG
#if __DEBUG
print("Former shape deleted (assign move)");
#endif
delete[] data;
}
if (refcount != nullptr && refcount != shape.refcount){
#ifdef __DEBUG
#if __DEBUG
print("Former shape refcount freed (assign move)");
#endif
delete refcount;
@@ -229,7 +227,7 @@ namespace np {
length = shape.length;
data = shape.data;
refcount = shape.refcount;
#ifdef __DEBUG
#if __DEBUG
total = shape.total;
if (refcount == nullptr)
print("Assigned copy shape has null refcount");
@@ -244,7 +242,7 @@ namespace np {
__host__ __device__
constexpr size_t& operator[](const size_t& i) const {
#ifdef __DEBUG
#if __DEBUG
if (i > length){
printf("Index %lu out of shape length %lu\n", i, length);
#ifndef __CUDACC__
@@ -258,7 +256,7 @@ namespace np {
constexpr bool operator==(const Shape& other) const noexcept {
if (length != other.length)
return false;
#ifdef __DEBUG
#if __DEBUG
if (total != other.total)
return false;
#endif
@@ -284,42 +282,42 @@ namespace np {
__host__ __device__
Array() noexcept {
// #ifdef __DEBUG
// #if __DEBUG
// print("Array created (default)");
// #endif
}
__host__ __device__
Array(const Shape& shape, T* data) noexcept : shape(shape), data(data), refcount(new size_t(1)) {
// #ifdef __DEBUG
// #if __DEBUG
// print("Array created (raw, copy shape)");
// #endif
}
__host__ __device__
Array(const Shape& shape) noexcept : shape(shape), data(new T[np::prod(shape)]), refcount(new size_t(1)) {
// #ifdef __DEBUG
// #if __DEBUG
// print("Array created (raw empty, copy shape)");
// #endif
}
__host__ __device__
Array(Shape&& shape, T* data) noexcept : shape(std::move(shape)), data(data), refcount(new size_t(1)) {
// #ifdef __DEBUG
// #if __DEBUG
// print("Array created (raw, move shape)");
// #endif
}
__host__ __device__
Array(Shape&& shape) noexcept : shape(std::move(shape)), data(new T[np::prod(shape)]), refcount(new size_t(1)) {
// #ifdef __DEBUG
// #if __DEBUG
// print("Array created (raw empty, move shape)");
// #endif
}
__host__ __device__
Array(const Array& array) noexcept : shape(array.shape) {
#ifdef __DEBUG
#if __DEBUG
print("Array created (copy)");
#endif
if (data != nullptr && data != array.data){
@@ -329,7 +327,7 @@ namespace np {
delete[] data;
}
if (refcount != nullptr && refcount != array.refcount){
#ifdef __DEBUG
#if __DEBUG
print("Former array refcount freed (move)");
#endif
delete refcount;
@@ -344,7 +342,7 @@ namespace np {
refcount = array.refcount;
if (refcount != nullptr)
(*refcount)++;
#ifdef __DEBUG
#if __DEBUG
else
print("Moved array has null refcount");
#endif
@@ -352,17 +350,17 @@ namespace np {
__host__ __device__
Array(Array&& array) noexcept {
// #ifdef __DEBUG
// #if __DEBUG
// print("Array created (move)");
// #endif
if (data != nullptr && data != array.data){
#ifdef __DEBUG
#if __DEBUG
print("Former array deleted (move)");
#endif
delete[] data;
}
if (refcount != nullptr && refcount != array.refcount){
#ifdef __DEBUG
#if __DEBUG
print("Former array refcount freed (move)");
#endif
delete refcount;
@@ -378,24 +376,24 @@ namespace np {
__host__ __device__
~Array() noexcept {
if(refcount == nullptr){
// #ifdef __DEBUG
// #if __DEBUG
// print("Array refcount freed more than once");
// #endif
return;
}
--(*refcount);
// #ifdef __DEBUG
// #if __DEBUG
// printf("Array destructed : %lu\n", *refcount);
// #endif
if(*refcount == 0){
if (data != nullptr){
delete[] data;
data = nullptr;
// #ifdef __DEBUG
// #if __DEBUG
// print("Array freeing ...");
// #endif
}
#ifdef __DEBUG
#if __DEBUG
else
printf("Array freed more than once : %lu\n", *refcount);
#endif
@@ -406,17 +404,17 @@ namespace np {
__host__ __device__
Array& operator=(const Array& array) noexcept {
#ifdef __DEBUG
#if __DEBUG
print("Array created (assign copy)");
#endif
if (data != nullptr && data != array.data){
#ifdef __DEBUG
#if __DEBUG
print("Former array deleted (assign copy)");
#endif
delete[] data;
}
if (refcount != nullptr && refcount != array.refcount){
#ifdef __DEBUG
#if __DEBUG
print("Former array refcount freed (assign copy)");
#endif
delete refcount;
@@ -433,7 +431,7 @@ namespace np {
refcount = array.refcount;
if (refcount != nullptr)
(*refcount)++;
#ifdef __DEBUG
#if __DEBUG
else
print("Assigned array has null refcount");
#endif
@@ -442,17 +440,17 @@ namespace np {
__host__ __device__
Array& operator=(Array&& array) noexcept {
// #ifdef __DEBUG
// #if __DEBUG
// print("Array created (assign move)");
// #endif
if (data != nullptr && data != array.data){
#ifdef __DEBUG
#if __DEBUG
print("Former array deleted (assign move)");
#endif
delete[] data;
}
if (refcount != nullptr && refcount != array.refcount){
#ifdef __DEBUG
#if __DEBUG
print("Former array refcount freed (assign move)");
#endif
delete refcount;
@@ -522,7 +520,7 @@ namespace np {
template<typename T>
__host__ __device__
constexpr T& Array<T>::operator[](const size_t& i) const {
#ifdef __DEBUG
#if __DEBUG
if (i > shape.total){
printf("Index %lu out of array size %lu\n", i, shape.total);
#ifndef __CUDACC__
@@ -563,7 +561,7 @@ namespace np {
template<typename T>
template<typename F>
Array<T> Array<T>::operator*(const Array<F>& other) const {
#ifdef __DEBUG
#if __DEBUG
if (shape != other.shape){
printf("Incompatible shapes\n");
throw;
@@ -598,7 +596,7 @@ namespace np {
template<typename T>
template<typename F>
Array<T>& Array<T>::operator*=(const Array<F>& other) {
#ifdef __DEBUG
#if __DEBUG
if (shape != other.shape){
printf("Incompatible shapes\n");
throw;
@@ -613,7 +611,7 @@ namespace np {
template<typename T>
template<typename F>
Array<T>& Array<T>::operator+=(const Array<F>& other) {
#ifdef __DEBUG
#if __DEBUG
if (shape != other.shape){
printf("Incompatible shapes\n");
throw;
@@ -638,7 +636,7 @@ namespace np {
template<typename T>
template<typename F>
Array<T> Array<T>::operator-(const np::Array<F>& other) const {
#ifdef __DEBUG
#if __DEBUG
if (shape != other.shape){
printf("Incompatible shapes\n");
throw;
@@ -921,7 +919,7 @@ np::Array<T> copyToDevice(const char* name, const np::Array<T>& array) noexcept
_print_cuda_error_(name, cudaMemcpy(d_array.data, array.data, array_size, cudaMemcpyHostToDevice));
//_print_cuda_error_(name, cudaMemcpy(d_array.shape.refcount, array.shape.refcount, sizeof(size_t), cudaMemcpyHostToDevice));
_print_cuda_error_(name, cudaMemcpy(d_array.shape.data, array.shape.data, shape_size, cudaMemcpyHostToDevice));
#ifdef __DEBUG
#if __DEBUG
d_array.shape.total = np::prod(array.shape);
#endif
return d_array;