Created
August 12, 2014 04:42
-
-
Save cancan101/654c070ef7b4bf34aac1 to your computer and use it in GitHub Desktop.
Linear Interpolator Test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"metadata": { | |
"name": "", | |
"signature": "sha256:da4ea90ada892513eeff8a76e55c15edf89286436f581696c117d33a8bfa2daa" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"import numpy as np\n", | |
"\n", | |
"import os\n", | |
"import pyopencl as cl\n", | |
"import pyopencl.array\n", | |
"\n", | |
"%load_ext pyopencl.ipython_ext" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 1 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# This following line should only be uncommented if you know the device is #1\n", | |
"# os.environ[\"PYOPENCL_CTX\"]=\"1\"" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 2 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"ctx = cl.create_some_context(interactive=True)\n", | |
"queue = cl.CommandQueue(ctx, \n", | |
" properties=cl.command_queue_properties.PROFILING_ENABLE)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"Choose device(s):\n", | |
"[0] <pyopencl.Device 'Intel(R) HD Graphics 4400' on 'Intel(R) OpenCL' at 0x310850>\n", | |
"[1] <pyopencl.Device 'Intel(R) Core(TM) i5-4300U CPU @ 1.90GHz' on 'Intel(R) OpenCL' at 0x9399470>\n" | |
] | |
}, | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"Choice, comma-separated [0]:0\n" | |
] | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"Set the environment variable PYOPENCL_CTX='0' to avoid being asked again.\n" | |
] | |
} | |
], | |
"prompt_number": 33 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"test = np.arange(10).astype(np.float32)*10" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 34 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"out1 = cl.array.empty(queue, (test.size,), dtype=np.float32)\n", | |
"out2 = cl.array.empty(queue, (test.size,), dtype=np.float32)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 35 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"print test\n", | |
"print out1\n", | |
"print out2" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"[ 0. 10. 20. 30. 40. 50. 60. 70. 80. 90.]\n", | |
"[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", | |
"[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n" | |
] | |
} | |
], | |
"prompt_number": 36 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"testImg = cl.image_from_array(ctx, np.ascontiguousarray(test))" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 37 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"%%cl_kernel \n", | |
"\n", | |
"__kernel \n", | |
"void interp1d(__read_only image1d_t testImg,\n", | |
" __global float* out1,\n", | |
" __global float* out2)\n", | |
"{\n", | |
" const sampler_t sampler = CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_CLAMP_TO_EDGE | CLK_FILTER_LINEAR;\n", | |
"\n", | |
" const size_t localId = get_local_id(0);\n", | |
" \n", | |
" out1[localId] = (float)(0.5f+0.1f*localId);\n", | |
" out2[localId] = read_imagef(testImg, sampler, (float)(0.5f+0.1f*localId)).x; \n", | |
"}" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 40 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"event = interp1d(queue, (out1.size,), None, testImg, out1.data, out2.data)\n", | |
"\n", | |
"print out1.get()\n", | |
"print out2.get()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"[ 0.5 0.60000002 0.69999999 0.80000001 0.89999998 1.\n", | |
" 1.10000002 1.20000005 1.29999995 1.4000001 ]\n", | |
"[ 0. 1.015625 1.9921875 3.0078125 3.984375 5. 6.015625\n", | |
" 6.9921875 8.0078125 8.984375 ]\n" | |
] | |
} | |
], | |
"prompt_number": 41 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment