diff --git a/build/cuda.cu.o b/build/cuda.cu.o index 21f4bd1..b698d94 100644 Binary files a/build/cuda.cu.o and b/build/cuda.cu.o differ diff --git a/build/tensor.o b/build/tensor.o index d3962d9..53855b4 100644 Binary files a/build/tensor.o and b/build/tensor.o differ diff --git a/norch/__init__.py b/norch/__init__.py index 355e312..f136503 100644 --- a/norch/__init__.py +++ b/norch/__init__.py @@ -5,6 +5,6 @@ from .utils import * from .norchvision import * from .utils import * -__version__ = "0.0.5" +__version__ = "0.0.7" __author__ = 'Lucas de Lima Nogueira' __credits__ = 'Lucas de Lima Nogueira' \ No newline at end of file diff --git a/norch/__pycache__/tensor.cpython-38.pyc b/norch/__pycache__/tensor.cpython-38.pyc index c7cc044..c48a006 100644 Binary files a/norch/__pycache__/tensor.cpython-38.pyc and b/norch/__pycache__/tensor.cpython-38.pyc differ diff --git a/norch/csrc/tensor.cpp b/norch/csrc/tensor.cpp index 9443a6f..1b9ed56 100644 --- a/norch/csrc/tensor.cpp +++ b/norch/csrc/tensor.cpp @@ -49,37 +49,30 @@ extern "C" { void delete_tensor(Tensor* tensor) { if (tensor != NULL) { - - if (tensor->shape != NULL) { - free(tensor->shape); - tensor->shape = NULL; - } - - if (tensor->data != NULL) { - if (strcmp(tensor->device, "cpu") == 0) { - free(tensor->data); - } else { - free_cuda(tensor->data); - } - tensor->data = NULL; - } - - if (tensor->device != NULL) { - free(tensor->device); - tensor->device = NULL; - } - - if (tensor->strides != NULL) { - free(tensor->strides); - tensor->strides = NULL; - } - free(tensor); + tensor = NULL; + } + } + + void delete_shape(Tensor* tensor) { + if (tensor->shape != NULL) { + free(tensor->shape); + tensor->shape = NULL; + } + } + + void delete_data(Tensor* tensor) { + if (tensor->data != NULL) { + if (strcmp(tensor->device, "cpu") == 0) { + free(tensor->data); + } else { + free_cuda(tensor->data); + } + tensor->data = NULL; } } void delete_strides(Tensor* tensor) { - // as strides always are allocated within C code, it must be handled separatedly if (tensor->strides != NULL) { free(tensor->strides); tensor->strides = NULL; @@ -87,7 +80,6 @@ extern "C" { } void delete_device(Tensor* tensor) { - // as device always are allocated within C code, it must be handled separatedly if (tensor->device != NULL) { free(tensor->device); tensor->device = NULL; @@ -114,20 +106,28 @@ extern "C" { int device_id = 0; char* endptr; + char* target_device_type; + long num = strtol(target_device, &endptr, 10); if (*endptr == '\0') { device_id = (int)num; - target_device = new char[strlen("cuda") + 1]; - strcpy(target_device, "cuda"); + target_device_type = new char[strlen("cuda") + 1]; + strcpy(target_device_type, "cuda"); + } + else { + target_device_type = new char[strlen("cuda") + 1]; + strcpy(target_device_type, "cpu"); } - if ((strcmp(target_device, "cuda") == 0) && (strcmp(tensor->device, "cpu") == 0)) { + if ((strcmp(target_device_type, "cuda") == 0) && (strcmp(tensor->device, "cpu") == 0)) { cpu_to_cuda(tensor, device_id); } - else if ((strcmp(target_device, "cpu") == 0) && (strcmp(tensor->device, "cuda") == 0)) { + else if ((strcmp(target_device_type, "cpu") == 0) && (strcmp(tensor->device, "cuda") == 0)) { cuda_to_cpu(tensor); } + + free(target_device_type); } Tensor* add_tensor(Tensor* tensor1, Tensor* tensor2) { diff --git a/norch/csrc/tensor.h b/norch/csrc/tensor.h index 6793e10..bb07c2a 100644 --- a/norch/csrc/tensor.h +++ b/norch/csrc/tensor.h @@ -14,6 +14,8 @@ extern "C" { Tensor* create_tensor(float* data, int* shape, int ndim, char* device); void delete_tensor(Tensor* tensor); void delete_strides(Tensor* tensor); + void delete_shape(Tensor* tensor); + void delete_strides(Tensor* tensor); void delete_device(Tensor* tensor); float get_item(Tensor* tensor, int* indices); Tensor* add_tensor(Tensor* tensor1, Tensor* tensor2); diff --git a/norch/libtensor.so b/norch/libtensor.so index 598e064..da03d6e 100755 Binary files a/norch/libtensor.so and b/norch/libtensor.so differ diff --git a/norch/tensor.py b/norch/tensor.py index d2048b1..263a177 100644 --- a/norch/tensor.py +++ b/norch/tensor.py @@ -83,8 +83,7 @@ class Tensor: def __del__(self): if hasattr(self, '_data_ctype') and self._data_ctype is not None: - # tensor created by user (ctypes) will be deleted by python garbage collector - # only strides need to be deallocated manually because it is created inside C code + Tensor._C.delete_strides.argtypes = [ctypes.POINTER(CTensor)] Tensor._C.delete_strides.restype = None Tensor._C.delete_strides(self.tensor) @@ -93,8 +92,26 @@ class Tensor: Tensor._C.delete_device.restype = None Tensor._C.delete_device(self.tensor) + Tensor._C.delete_tensor.argtypes = [ctypes.POINTER(CTensor)] + Tensor._C.delete_tensor.restype = None + Tensor._C.delete_tensor(self.tensor) + elif self.tensor is not None: - # tensor created during operations must be deallocated + Tensor._C.delete_strides.argtypes = [ctypes.POINTER(CTensor)] + Tensor._C.delete_strides.restype = None + Tensor._C.delete_strides(self.tensor) + + Tensor._C.delete_data.argtypes = [ctypes.POINTER(CTensor)] + Tensor._C.delete_data.restype = None + Tensor._C.delete_data(self.tensor) + + Tensor._C.delete_shape.argtypes = [ctypes.POINTER(CTensor)] + Tensor._C.delete_shape.restype = None + Tensor._C.delete_shape(self.tensor) + + Tensor._C.delete_device.argtypes = [ctypes.POINTER(CTensor)] + Tensor._C.delete_device.restype = None + Tensor._C.delete_device(self.tensor) Tensor._C.delete_tensor.argtypes = [ctypes.POINTER(CTensor)] Tensor._C.delete_tensor.restype = None diff --git a/setup.py b/setup.py index 2dfaca7..74fed5b 100644 --- a/setup.py +++ b/setup.py @@ -76,7 +76,7 @@ class CustomInstall(install): setuptools.setup( name="norch", - version="0.0.5", + version="0.0.7", author="Lucas de Lima", author_email="nogueiralucasdelima@gmail.com", description="A deep learning framework",