MatrixMul code error: identifier "MATRIX_SIZE" is undefined

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

MatrixMul code error: identifier "MATRIX_SIZE" is undefined

maxrider11
This post was updated on .
Hi ,
Just installed pycuda and tried to run matrixmul code from here. Got error in the phase where we compile kernel code . This is what i did:

>>> #!/usr/bin/env python
... #-*- coding: utf-8 -*-
... import numpy as np
>>> from pycuda import driver, compiler, gpuarray, tools
>>>  # -- initialize the device
>>> import pycuda.autoinit
>>> kernel_code_template = """
... __global__ void MatrixMulKernel (float *a, float *b, float *c)
... {
...    int tx = threadIdx.x;
...    int ty = threadIdx.y;
...    float Pvalue=0;
...    for (int k=0;k<(MATRIX_SIZE)s;++k){
...      float Aelement = a[ty * %(MATRIX_SIZE)s + k];
...      float Belement = b[k * %(MATRIX_SIZE)s + tx];
...      Pvalue += Aelement + Belement;
...    }
...    c[ty * %(MATRIX_SIZE)s + tx] = Pvalue;
... }
... """
>>> MATRIX_SIZE =2
>>> a_cpu = np.random.randn(MATRIX_SIZE,MATRIX_SIZE).astype(np.float32)
>>> b_cpu = np.random.randn(MATRIX_SIZE, MATRIX_SIZE).astype(np.float32)
>>> c_cpu = np.dot(a_cpu, b_cpu)
>>> a_gpu = gpuarray.to_gpu(a_cpu)
>>> b_gpu = gpuarray.to_gpu(b_cpu)
>>> c_gpu = gpuarray.empty((MATRIX_SIZE,MATRIX_SIZE),np.float32)
>>> kernel_code = kernel_code_template %{
... 'MATRIX_SIZE':MATRIX_SIZE
... }
>>> mod = compiler.SourceModule(kernel_code)

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/pycuda-2011.2.2-py2.7-linux-x86_64.egg/pycuda/compiler.py", line 283, in __init__
    arch, code, cache_dir, include_dirs)
  File "/usr/local/lib/python2.7/dist-packages/pycuda-2011.2.2-py2.7-linux-x86_64.egg/pycuda/compiler.py", line 273, in compile
    return compile_plain(source, options, keep, nvcc, cache_dir)
  File "/usr/local/lib/python2.7/dist-packages/pycuda-2011.2.2-py2.7-linux-x86_64.egg/pycuda/compiler.py", line 129, in compile_plain
    cmdline, stdout=stdout, stderr=stderr)
pycuda.driver.CompileError: nvcc compilation of /tmp/tmpGuRUsm/kernel.cu failed
[command: nvcc --cubin -arch sm_11 -I/usr/local/lib/python2.7/dist-packages/pycuda-2011.2.2-py2.7-linux-x86_64.egg/pycuda/../include/pycuda kernel.cu]
[stderr:
kernel.cu(8): error: identifier "MATRIX_SIZE" is undefined

kernel.cu(8): error: expected a ";"

2 errors detected in the compilation of "/tmp/tmpxft_00007bbf_00000000-4_kernel.cpp4.ii".
]



If anyone can figure error, it will be appreciated.

*edit: I just copied code in notepad file which i named umustrun.py. It is running. But why error when i write step by step like above ?