Discussion:
[PyCUDA] C++ style comment yields bad code. Bug?
金陆
2014-03-16 04:17:30 UTC
Permalink
Hi, everyone
There is a simple code, but I found if I use "//CUPRINTF("\n\n");" in test.py, the yielded kernel.cu file is invalid. That is so strange, because 1."//unsigned int y=threadIdx.y; " dose not ruin the yielded kernel.cu; 2. if I use "/*CUPRINTF("\n\n");*/", there is no problem

I am using pycuda-2013.1.1.win32-py2.7.exe from http://www.lfd.uci.edu/~gohlke/pythonlibs/ on Winxp

Thanks in advance

[test.py starts]
#coding=utf-8
import pycuda.driver as drv
import pycuda.autoinit
from pycuda.compiler import SourceModule
def compile_kernel(kernel_code, kernel_name):
mod = SourceModule(kernel_code)
func = mod.get_function(kernel_name)
return func
my_kernel_code = """
#include "math.h"
#include "stdio.h"
__global__ void my_kernel()
{
unsigned int x=threadIdx.x;
//unsigned int y=threadIdx.y;
//CUPRINTF("\n\n");
}
"""
kernel = compile_kernel(my_kernel_code, 'my_kernel')
if __name__ == '__main__':
pass
[test.py ends]


[kernel.cu starts]
extern "C" {
#include "math.h"
#include "stdio.h"
__global__ void my_kernel()
{
unsigned int x=threadIdx.x;
//unsigned int y=threadIdx.y;
//CUPRINTF("
");
}
}
[kernel.cu ends]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.tiker.net/pipermail/pycuda/attachments/20140316/6044bf3b/attachment.html>
Bogdan Opanchuk
2014-03-16 04:40:19 UTC
Permalink
Hi 金陆,

The "\n\n" in your code correspond to two actual newlines in the .cu
file being compiled, not to a "\n\n" string, because they are resolved
by the Python interpreter at the parsing stage. See the kernel.cu you
quoted for the result, you have 'CUPRINTF("' commented and an
unmatched quote and parenthesis out of the comment — that's why the
compiler is complaining. You could actually just "print
my_kernel_code" inside the Python program and see that the result is
not valid C code.

In order to make "\n\n" appear in the C code, you can either prefix
the my_kernel_code literal with 'r' (my_kernel_code = r""" ... your
code here ... """), preventing the interpreter from resolving
character codes, or prefix each backslash with another backslash, so
that it gets resolved as a single backslash (my_kernel_code = """ ...
CUPRINTF("\\n\\n"); """).
Post by 金陆
Hi, everyone
There is a simple code, but I found if I use "//CUPRINTF("\n\n");" in
test.py, the yielded kernel.cu file is invalid. That is so strange, because
1."//unsigned int y=threadIdx.y; " dose not ruin the yielded kernel.cu; 2.
if I use "/*CUPRINTF("\n\n");*/", there is no problem
I am using pycuda-2013.1.1.win32-py2.7.exe from
http://www.lfd.uci.edu/~gohlke/pythonlibs/ on Winxp
Thanks in advance
[test.py starts]
#coding=utf-8
import pycuda.driver as drv
import pycuda.autoinit
from pycuda.compiler import SourceModule
mod = SourceModule(kernel_code)
func = mod.get_function(kernel_name)
return func
my_kernel_code = """
#include "math.h"
#include "stdio.h"
__global__ void my_kernel()
{
unsigned int x=threadIdx.x;
//unsigned int y=threadIdx.y;
//CUPRINTF("\n\n");
}
"""
kernel = compile_kernel(my_kernel_code, 'my_kernel')
pass
[test.py ends]
[kernel.cu starts]
extern "C" {
#include "math.h"
#include "stdio.h"
__global__ void my_kernel()
{
unsigned int x=threadIdx.x;
//unsigned int y=threadIdx.y;
//CUPRINTF("
");
}
}
[kernel.cu ends]
_______________________________________________
PyCUDA mailing list
PyCUDA at tiker.net
http://lists.tiker.net/listinfo/pycuda
Loading...