Fix pow create another vector
This commit is contained in:
parent
8366245813
commit
b09b9b9ba0
13 changed files with 53 additions and 17 deletions
BIN
build/cpu.o
BIN
build/cpu.o
Binary file not shown.
BIN
build/cuda.cu.o
BIN
build/cuda.cu.o
Binary file not shown.
Binary file not shown.
BIN
build/tensor.o
BIN
build/tensor.o
Binary file not shown.
|
|
@ -25,9 +25,9 @@ void elementwise_mul_tensor_cpu(Tensor* tensor1, Tensor* tensor2, float* result_
|
|||
}
|
||||
}
|
||||
|
||||
void pow_tensor_cpu(Tensor* tensor, float power) {
|
||||
void pow_tensor_cpu(Tensor* tensor, float power, float* result_data) {
|
||||
|
||||
for (int i = 0; i < tensor->size; i++) {
|
||||
tensor->data[i] = powf(tensor->data[i], power);
|
||||
result_data[i] = powf(tensor->data[i], power);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,4 +2,4 @@ void add_tensor_cpu(Tensor* tensor1, Tensor* tensor2, float* result_data);
|
|||
void sub_tensor_cpu(Tensor* tensor1, Tensor* tensor2, float* result_data);
|
||||
void elementwise_mul_tensor_cpu(Tensor* tensor1, Tensor* tensor2, float* result_data);
|
||||
|
||||
void pow_tensor_cpu(Tensor* tensor, float power);
|
||||
void pow_tensor_cpu(Tensor* tensor, float power, float* result_data);
|
||||
|
|
@ -97,17 +97,17 @@ __host__ void elementwise_mul_tensor_cuda(Tensor* tensor1, Tensor* tensor2, floa
|
|||
cudaDeviceSynchronize();
|
||||
}
|
||||
|
||||
__global__ void pow_tensor_cuda_kernel(float* data, float power, int size) {
|
||||
__global__ void pow_tensor_cuda_kernel(float* data, float power, float* result_data, int size) {
|
||||
int i = blockIdx.x * blockDim.x + threadIdx.x;
|
||||
if (i < size) {
|
||||
data[i] = powf(data[i], power);
|
||||
result_data[i] = powf(data[i], power);
|
||||
}
|
||||
}
|
||||
|
||||
__host__ void pow_tensor_cuda(Tensor* tensor, float power) {
|
||||
__host__ void pow_tensor_cuda(Tensor* tensor, float power, float* result_data) {
|
||||
|
||||
int number_of_blocks = (tensor->size + THREADS_PER_BLOCK - 1) / THREADS_PER_BLOCK;
|
||||
pow_tensor_cuda_kernel<<<number_of_blocks, THREADS_PER_BLOCK>>>(tensor->data, power, tensor->size);
|
||||
pow_tensor_cuda_kernel<<<number_of_blocks, THREADS_PER_BLOCK>>>(tensor->data, power, result_data, tensor->size);
|
||||
|
||||
cudaError_t error = cudaGetLastError();
|
||||
if (error != cudaSuccess) {
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@
|
|||
__global__ void elementwise_mul_tensor_cuda_kernel(float* data1, float* data2, float* result_data, int size);
|
||||
__host__ void elementwise_mul_tensor_cuda(Tensor* tensor1, Tensor* tensor2, float* result_data);
|
||||
|
||||
__global__ void pow_tensor_cuda_kernel(float* data, float power, int size);
|
||||
__host__ void pow_tensor_cuda(Tensor* tensor, float power);
|
||||
__global__ void pow_tensor_cuda_kernel(float* data, float power, float* result_data, int size);
|
||||
__host__ void pow_tensor_cuda(Tensor* tensor, float power, float* result_data);
|
||||
|
||||
#endif /* CUDA_KERNEL_H_ */
|
||||
|
|
|
|||
|
|
@ -304,12 +304,40 @@ extern "C" {
|
|||
return create_tensor(result_data, shape, ndim, device);
|
||||
}
|
||||
|
||||
void pow_tensor(Tensor* tensor, float power) {
|
||||
Tensor* pow_tensor(Tensor* tensor, float power) {
|
||||
char* device = (char*)malloc(strlen(tensor->device) + 1);
|
||||
if (device != NULL) {
|
||||
strcpy(device, tensor->device);
|
||||
} else {
|
||||
fprintf(stderr, "Memory allocation failed\n");
|
||||
exit(-1);
|
||||
}
|
||||
int ndim = tensor->ndim;
|
||||
int* shape = (int*)malloc(ndim * sizeof(int));
|
||||
if (shape == NULL) {
|
||||
fprintf(stderr, "Memory allocation failed\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
for (int i = 0; i < ndim; i++) {
|
||||
shape[i] = tensor->shape[i];
|
||||
}
|
||||
|
||||
if (strcmp(tensor->device, "cuda") == 0) {
|
||||
pow_tensor_cuda(tensor, power);
|
||||
|
||||
float* result_data;
|
||||
cudaMalloc((void **)&result_data, tensor->size * sizeof(float));
|
||||
pow_tensor_cuda(tensor, power, result_data);
|
||||
return create_tensor(result_data, shape, ndim, device);
|
||||
}
|
||||
else {
|
||||
pow_tensor_cpu(tensor, power);
|
||||
float* result_data = (float*)malloc(tensor->size * sizeof(float));
|
||||
if (result_data == NULL) {
|
||||
fprintf(stderr, "Memory allocation failed\n");
|
||||
exit(1);
|
||||
}
|
||||
pow_tensor_cpu(tensor, power, result_data);
|
||||
return create_tensor(result_data, shape, ndim, device);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@ extern "C" {
|
|||
Tensor* elementwise_mul_tensor(Tensor* tensor1, Tensor* tensor2);
|
||||
void reshape_tensor(Tensor* tensor, int* new_shape, int new_ndim);
|
||||
Tensor* matmul_tensor(Tensor* tensor1, Tensor* tensor2);
|
||||
void pow_tensor(Tensor* tensor, float power);
|
||||
Tensor* pow_tensor(Tensor* tensor, float power);
|
||||
void to_device(Tensor* tensor, char* device);
|
||||
}
|
||||
|
||||
|
|
|
|||
Binary file not shown.
|
|
@ -204,8 +204,14 @@ class Tensor:
|
|||
power = ctypes.c_float(power)
|
||||
|
||||
Tensor._C.pow_tensor.argtypes = [ctypes.POINTER(CTensor), ctypes.c_float]
|
||||
Tensor._C.pow_tensor.restype = None
|
||||
Tensor._C.pow_tensor.restype = ctypes.POINTER(CTensor)
|
||||
|
||||
Tensor._C.pow_tensor(self.tensor, power)
|
||||
result_tensor_ptr = Tensor._C.pow_tensor(self.tensor, power)
|
||||
|
||||
return self
|
||||
result_data = Tensor()
|
||||
result_data.tensor = result_tensor_ptr
|
||||
result_data.shape = self.shape.copy()
|
||||
result_data.ndim = self.ndim
|
||||
result_data.device = self.device
|
||||
|
||||
return result_data
|
||||
4
test.py
4
test.py
|
|
@ -28,7 +28,9 @@ if __name__ == "__main__":
|
|||
#c = Tensor([[1, 2, 3], [1, 2, 3], [1, 2, 3]])#.to("cuda")
|
||||
|
||||
#d = b-c
|
||||
print(a ** 2)
|
||||
|
||||
b = a ** 2
|
||||
print(b)
|
||||
#print(a ** 2)
|
||||
|
||||
"""#print(a)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue