85 lines
4.5 KiB
Plaintext
85 lines
4.5 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"ename": "TypeError",
|
|
"evalue": "No registered converter was able to produce a C++ rvalue of type unsigned int from this Python object of type numpy.int64",
|
|
"output_type": "error",
|
|
"traceback": [
|
|
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
|
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
|
|
"Cell \u001b[0;32mIn[1], line 25\u001b[0m\n\u001b[1;32m 22\u001b[0m drv\u001b[38;5;241m.\u001b[39mmemcpy_htod(y_gpu, y)\n\u001b[1;32m 24\u001b[0m \u001b[38;5;66;03m# 调用函数\u001b[39;00m\n\u001b[0;32m---> 25\u001b[0m \u001b[43madd_func\u001b[49m\u001b[43m(\u001b[49m\u001b[43mn\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mx_gpu\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43my_gpu\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mblock\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m256\u001b[39;49m\u001b[43m,\u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m,\u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mgrid\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mn\u001b[49m\u001b[38;5;241;43m/\u001b[39;49m\u001b[38;5;241;43m/\u001b[39;49m\u001b[38;5;241;43m256\u001b[39;49m\u001b[43m,\u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 27\u001b[0m \u001b[38;5;66;03m# 将结果复制回CPU\u001b[39;00m\n\u001b[1;32m 28\u001b[0m drv\u001b[38;5;241m.\u001b[39mmemcpy_dtoh(y, y_gpu)\n",
|
|
"File \u001b[0;32m~/anaconda3/envs/pycuda_3_10/lib/python3.10/site-packages/pycuda/driver.py:502\u001b[0m, in \u001b[0;36m_add_functionality.<locals>.function_call\u001b[0;34m(func, *args, **kwargs)\u001b[0m\n\u001b[1;32m 498\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mtime\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m time\n\u001b[1;32m 500\u001b[0m start_time \u001b[38;5;241m=\u001b[39m time()\n\u001b[0;32m--> 502\u001b[0m \u001b[43mfunc\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_launch_kernel\u001b[49m\u001b[43m(\u001b[49m\u001b[43mgrid\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mblock\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43marg_buf\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mshared\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m)\u001b[49m\n\u001b[1;32m 504\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m post_handlers \u001b[38;5;129;01mor\u001b[39;00m time_kernel:\n\u001b[1;32m 505\u001b[0m Context\u001b[38;5;241m.\u001b[39msynchronize()\n",
|
|
"\u001b[0;31mTypeError\u001b[0m: No registered converter was able to produce a C++ rvalue of type unsigned int from this Python object of type numpy.int64"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"import pycuda.autoinit\n",
|
|
"import pycuda.driver as drv\n",
|
|
"import numpy as np\n",
|
|
"\n",
|
|
"# 加载PTX文件\n",
|
|
"mod = drv.module_from_file(\"test.ptx\")\n",
|
|
"\n",
|
|
"# 获取函数\n",
|
|
"add_func = mod.get_function(\"add\")\n",
|
|
"\n",
|
|
"# 创建数据\n",
|
|
"n = np.uint32(100) # Convert to unsigned int\n",
|
|
"x = np.random.rand(n).astype(np.float32)\n",
|
|
"y = np.random.rand(n).astype(np.float32)\n",
|
|
"\n",
|
|
"# 分配内存\n",
|
|
"x_gpu = drv.mem_alloc(x.nbytes)\n",
|
|
"y_gpu = drv.mem_alloc(y.nbytes)\n",
|
|
"\n",
|
|
"# 将数据复制到GPU\n",
|
|
"drv.memcpy_htod(x_gpu, x)\n",
|
|
"drv.memcpy_htod(y_gpu, y)\n",
|
|
"\n",
|
|
"# 调用函数\n",
|
|
"add_func(x_gpu, y_gpu, block=(256,1,1), grid=(n//256,1))\n",
|
|
"\n",
|
|
"# 将结果复制回CPU\n",
|
|
"drv.memcpy_dtoh(y, y_gpu)\n",
|
|
"\n",
|
|
"# 检查结果\n",
|
|
"print(y)"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "pycuda_3_10",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.10.13"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 2
|
|
}
|