Globally visible buffers or direct memory access?

Hi, I try to create a host class that would generate kernel source code for its internal computations. At the time point of generating the source code I have buffers in gpu’s global memory that are to be passed to the kernel as arguments. However its some extra programming effort to generate the arguments list and it will also impact the performance since the arguments list is pretty long.
Looking for the workarounds I would like to ask following questions:

  1. is it possible to declare a global buffer so that it would be visible to the kernel without passing a pointer on it to the kernel as an argument?
  2. is possible to have something like this in the kernel’s source: *(0x24fd452) or *((0x24fd452)[gid]) - a kind of direct access without a pointer variable?

thank you in advance
qwer

  1. is it possible to declare a global buffer so that it would be visible to the kernel without passing a pointer on it to the kernel as an argument?

No, but that has not stopped people from trying to come up with clever workarounds. Be warned: the only method that will work in all implementations and is guaranteed to produce correct results is passing the buffers as kernel arguments. Don’t even think of anything else if you care about the stability and correctness of your software.

  1. is possible to have something like this in the kernel’s source: *(0x24fd452) or *((0x24fd452)[gid]) - a kind of direct access without a pointer variable?

Doing that sort of thing will never be portable across systems.

I’m sorry to be blunt. I’m simply trying to save you some headaches.

[quote:2uirr0jt]1) is it possible to declare a global buffer so that it would be visible to the kernel without passing a pointer on it to the kernel as an argument?

No, but that has not stopped people from trying to come up with clever workarounds. Be warned: the only method that will work in all implementations and is guaranteed to produce correct results is passing the buffers as kernel arguments. Don’t even think of anything else if you care about the stability and correctness of your software.[/quote:2uirr0jt]

I also had a couple ideas - but the problem with them that they impact performance - I would need one extra lookup to access my buffer (while doing it inderectly)… global variable would be better. Do you know any workaround without performance lost?

[quote:2uirr0jt]2) is possible to have something like this in the kernel’s source: *(0x24fd452) or *((0x24fd452)[gid]) - a kind of direct access without a pointer variable?

Doing that sort of thing will never be portable across systems.[/quote:2uirr0jt]

Why? remember I have a pointer on buffer, on each platform it will point to a different place, but all I will do is just convert its integer value into a string and paste into kernel’s code… why is it not portable?

But anyway - is it possible?

If you want to access a buffer object, pass it as a kernel argument. Don’t attempt workarounds: they may work on some driver versions and on some machines, and they will fail everywhere else.

why is it not portable?

Because you are assuming that the address of a buffer doesn’t change. It can change from one NDRange to the next.

I’m not stopping you from doing anything. I’m just saying that the only portable way to access a buffer is passing it as a kernel arguments. Anything else is a hack and will give you headaches in the future.

thank you very much! you saved me a lot of time and nerves.

should I request global kernel variables as a new feature or was it already discussed somewhere and rejected?

You can ask for it, sure. It was discussed and rejected IIRC, but if enough people want something like this I guess the group may talk about it again.

Personally I don’t think it is likely to be added; it would introduce a number of problems with thread safety, driver complexity, etc.

I also had a couple ideas - but the problem with them that they impact performance - I would need one extra lookup to access my buffer (while doing it inderectly)… global variable would be better. Do you know any workaround without performance lost?

I’m not sure exactly what you are doing here, but are you speculating or have you tried it and measured the performance? The performance cost of adding kernel arguments is going to be insignificant compared to the cost of executing your kernel. If it isn’t then I would question whether you should be doing your computation in a kernel at all. And the cost of having a constant global pointer vs. having an argument is going to be zero or extremely negligible because you are almost certainly memory bound on the access to the actual data.