Weird error on Kernel when using get_global_id(0)

Hello,

I’m trying to print to a buffer the work thread ID. To accomplish that, I’ve implemented the itoa, I adapted this version:

http://en.wikipedia.org/wiki/Itoa

It works perfectly for any kind of integer declared or hard-coded, except when I do:

int idx = get_global_id(0)

itoa(idx,idx_str);

When I do that, it crashes on enqueReadBuffer with error -5, and I can’t figure out what is the problem, because -5 do not match neither of:

CL_INVALID_COMMAND_QUEUE

CL_INVALID_CONTEXT

CL_INVALID_MEM_OBJECT

CL_INVALID_VALUE

CL_INVALID_EVENT_WAIT_LIST

CL_MEM_OBJECT_ALLOCATION_FAILURE

CL_OUT_OF_HOST_MEMORY

I thought that could be overflow or something like that, but I’ve tried with a 999999999999999 and it overflows the int but the itoa didn’t crash, only printed a overflowed number.

Any hint?

Thank you

Hard to say what the error might be without access to the actual kernel, but I can at least help you with the error value. All errors are listed in cl.h, which should be supplied with the OpenCL installation. Here you can find

#define CL_OUT_OF_RESOURCES						 -5

A way to translate for error codes to human-readable strings can be found at

http://forums.nvidia.com/index.php?s=&…st&p=945293

get_global_id returns a size_t. You might try an explicit cast.

thank you for your help guys, it looks that something went the loop to not finish. I’ve solved the problem, but I don’t know why its solved:P

I made one change in itoa() func:

Originally:

if (sign < 0)

	   s[i++] = '-';

	s[i] = '

if (sign < 0)

   s[i++] = '-';

s[i] = '\0';
';

After I only change the code to make the same thing but in another way:

if (sign < 0) {

	   s[i] = '-';

	   s[i+1] = '

if (sign < 0) {

   s[i] = '-';

   s[i+1] = '\0';

}else

  s[i] = '\0';
';

	}else

	  s[i] = '

if (sign < 0) {

   s[i] = '-';

   s[i+1] = '\0';

}else

  s[i] = '\0';
';

And it works…

Thank you

Hmm, are you sure you need i++ in first code, not ++i?

Yes I’m sure, because before this i++, ‘i’ was incremented, so if it wasn’t negative, its ready to receive the ‘\0’. But the change that I did, do exactly the same that the previous code, but works… It’s OK for now:P

Cound it be that in s[i++]="-" i was incremented first and only then s was assigned.

a=++i;
and
a=i++;

is there any differency?

It could be a bug in compiler, that need to be reported. Or c++ misunderstanding in wiki article. Something of this two.

first is “increment i, then assign it to a”

second is “assign i to a, then increment i”

i++ and ++i are quite different operators semantically, they have different precedence values and associativity. See http://en.wikipedia.org/wiki/Operators_in_…ator_precedence

The fun thing is that those differences don’t matter in many cases. This leads to subtle errors every once in a while, that’s C++ for you ;)

vandop’s two code snippets should be equivalent in C++. In s[i++] = a, the incrementation of i should be the last thing that happens.

vandop

Do you use value of i later?
Your codes are not equivalent, second example does not change i.

I know that is not equivalent, but for this case they are, because I don’t use ‘i’ anymore.

But take a look, if you copy paste wiki code and run it on CPU code or even in GPU code, but without passing get_global_id() size_t as parameter, it works well… The error its very very weird, as I said, I’ve tried to overflow the number manually and the itoa always worked, but when I started to call itoa(get_local_id(0)) it simply crashes… returning a OUT_OF_RESOURCES, what I thought that was a infinite loop somewhere… or an invalid access to an array position… don’t know, the run time error check does not help very much too:P

I tried everything, convert_to_int() and traditional casts…but nothing worked…