调用GPU的本质其实是调用CUDA的dll
如果你对CUDA编程不熟悉,可以参考CUDA并行编程概述
生成CUDA dll
调用显卡的方法是调用CUDA的dll,因此首先要使用CUDA生成dll
下面是示例CUDA代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| #include "cuda_runtime.h" #include "device_launch_parameters.h" #include <stdio.h> #include <iostream> using namespace std; __global__ void DoInKernel(int* a, int* b, int* c, int num) { int i = blockIdx.x * 1024 + threadIdx.x; if (i >= num) return; c[i] = a[i] + b[i]; } extern "C" _declspec(dllexport) void Add(int* ori_a, int* ori_b, int* ori_c, int num) { int* dev_a; int* dev_b; int* dev_c; int size = num * sizeof(int); cudaMalloc((void**)&dev_a, size); cudaMalloc((void**)&dev_b, size); cudaMalloc((void**)&dev_c, size); cudaMemcpy(dev_a, ori_a, size, cudaMemcpyHostToDevice); cudaMemcpy(dev_b, ori_b, size, cudaMemcpyHostToDevice); DoInKernel << <num / 1024 + 1, 1024 >> > (dev_a, dev_b, dev_c, num); cudaMemcpy(ori_c, dev_c, size, cudaMemcpyDeviceToHost); cudaFree(dev_a); cudaFree(dev_b); cudaFree(dev_c); }
|
它的作用是将 ori_a 和 ori_b 数组内的数组相加,并保存到 ori_c
编译后得到CUDA.dll文件
调用dll
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| import ctypes import random length = 4096
dll_path = r"C:\Users\Administrator\桌面\CUDA.dll"
dll = ctypes.cdll.LoadLibrary(dll_path)
c_int = ctypes.c_int * length array_a = c_int() array_b = c_int() array_c = c_int()
i = 0 while i < length: array_a[i] = random.randint(0,1000) array_b[i] = random.randint(0,1000) i = i + 1
dll.Add(array_a, array_b, array_c, 4096)
i = 0 while i < length: print(array_a[i],"+",array_b[i],"=",array_c[i]) i = i + 1
|
运行结果