topic kernel creation help in Archives Discussions
https://community.amd.com/t5/archives-discussions/kernel-creation-help/m-p/170540#M14841
<HTML><HEAD></HEAD><BODY><P>If you want to use a 2d range, for this problem you have to be careful not to write to the same address concurrently with another kernel (race condition). I guess you could output a matrix size*size with: matrix[i*size + j] = (a <EM></EM>* b<J>) to perform all the multiplications in parallel then reduce the diagonals.. Its still a horrible algorithmn since you´ll have load balancing problems which arise given the nature of the polinomial multiplication producing more values in the midrange values (~principal diagonal) given the higher quantity of possible permutations, and therefore youll have a few units doing most of the work. Added to that youll have uncoalesced acesses along the diagonal, making the parallel reduction performance dismal. Developing an algorithmn to make this better than on a cpu - especially considering transfer costs - is not straightforward. Nevertheless, im sure you can find some literature online on this, since doing the same operation on openmp, for example, will produce the same race condition drawback.</J></P></BODY></HTML>Mon, 19 Jul 2010 18:58:25 GMTfpaboim2010-07-19T18:58:25Zkernel creation help
https://community.amd.com/t5/archives-discussions/kernel-creation-help/m-p/170537#M14838
<HTML><HEAD></HEAD><BODY><P>I'm trying to figure out how to port some sequential code to a kernel so i can test the speedup, but im having a lot of trouble. None of the tutorials ive found help with this aspect, only the setting up the kernel to run and recieve data. So far i have this for multiplying polynomials together.</P><P>sequential:</P><P>int *mult(int size, int *a, int *b)<BR />{<BR /> int *out, i, j;<BR /> out = (int *)malloc(sizeof(int) * size * 2 - 1);<BR /> <BR /> for(i = 0; i < size; i++){<BR /> for(j = 0; j < size; j++){<BR /> out[i + j] = (a <I>* b<J>) + out[i + j];<BR /> }<BR /> }<BR /> return out;<BR /> <BR /> }</J></I></P><P><I> </I></P><P><I>Naive Kernel:</I></P><P><I> </I></P><P><I>#pragma OPENCL EXTENSION cl_khr_byte_addressable_store : enable<BR /><BR />__kernel void poly_mul( __global int *a, __global int *b, __global int *c)<BR />{<BR /> <BR /> size_t i = get_global_id(0);<BR /> size_t j = get_global_id(1);<BR /> <BR /> c[i + j] = (a * b<J>) + c[i + j]; <BR /> <BR /> }</J></I></P><P><I>this doesnt return the correct result, i think this is because it of the c[i + j] on the end. Anyone have any advice or a link to a good tutorial on this aspect of opencl?</I></P><P><I> </I></P><P><I>Thanks</I></P></BODY></HTML>Thu, 03 Jun 2010 12:05:04 GMThttps://community.amd.com/t5/archives-discussions/kernel-creation-help/m-p/170537#M14838Chocrates2010-06-03T12:05:04Zkernel creation help
https://community.amd.com/t5/archives-discussions/kernel-creation-help/m-p/170538#M14839
<HTML><HEAD></HEAD><BODY><P>I'm a beginner, but it seams to me that you don't protect memory:</P><P>for example, in your code both threads (1, 2) and (2, 1) write to the memory address c[3] at the same time different results, anything can end up there, or first result or second or a combination of the two (they are all in parallel so they do all the operations at the same time). So the result is just junk.</P></BODY></HTML>Fri, 16 Jul 2010 00:05:07 GMThttps://community.amd.com/t5/archives-discussions/kernel-creation-help/m-p/170538#M14839mihzaha2010-07-16T00:05:07Zkernel creation help
https://community.amd.com/t5/archives-discussions/kernel-creation-help/m-p/170539#M14840
<HTML><HEAD></HEAD><BODY><P>Yep, mihzaha is right. But he's missing an explanation of the underlying reason:</P><P>Data layout in memory: I assume your source memory is a 2d array of ints, correct? Well in memory (and after passing to your kernel) these items have to be laid out sequentially, and in C its done in what's called "Row Major" format. You need to adjust your indexing from c[i+j] to something along the lines of c[i*rowSize+j] to switch from 2d indexing to 1d.</P><P>Also, I presume you're doing a small problem size, so get_global_id is <EM>excusable</EM>, but you'll want to get used to using get_local_id and get_group_id. Once your problem size gets bigger you'll have to use multi-tiered indexing.</P></BODY></HTML>Fri, 16 Jul 2010 00:17:23 GMThttps://community.amd.com/t5/archives-discussions/kernel-creation-help/m-p/170539#M14840psath2010-07-16T00:17:23Zkernel creation help
https://community.amd.com/t5/archives-discussions/kernel-creation-help/m-p/170540#M14841
<HTML><HEAD></HEAD><BODY><P>If you want to use a 2d range, for this problem you have to be careful not to write to the same address concurrently with another kernel (race condition). I guess you could output a matrix size*size with: matrix[i*size + j] = (a <EM></EM>* b<J>) to perform all the multiplications in parallel then reduce the diagonals.. Its still a horrible algorithmn since you´ll have load balancing problems which arise given the nature of the polinomial multiplication producing more values in the midrange values (~principal diagonal) given the higher quantity of possible permutations, and therefore youll have a few units doing most of the work. Added to that youll have uncoalesced acesses along the diagonal, making the parallel reduction performance dismal. Developing an algorithmn to make this better than on a cpu - especially considering transfer costs - is not straightforward. Nevertheless, im sure you can find some literature online on this, since doing the same operation on openmp, for example, will produce the same race condition drawback.</J></P></BODY></HTML>Mon, 19 Jul 2010 18:58:25 GMThttps://community.amd.com/t5/archives-discussions/kernel-creation-help/m-p/170540#M14841fpaboim2010-07-19T18:58:25Zkernel creation help
https://community.amd.com/t5/archives-discussions/kernel-creation-help/m-p/170541#M14842
<HTML><HEAD></HEAD><BODY><P>Wow, I completely blew it on my first look at this problem. I blame lack of coffee <IMG src="https://community.amd.com/legacyfs/online/emoticons/silly.png" /></P><P>Algorithmically, your kernel is correct, but fpaboim hit the nail on the head. This problem results in a lot of threads trying to write to the same memory address simultaneously and bashing each other's data.</P><P>For polynomials of lower order you could probably get away with using atomic adds for the <EM>c[i + j] = (a</EM> * b<J>) + c[i + j]; calculation.</J></P><P>Is there any way you can map the algorithm so that each thread writes to a unique memory location and then afterwards call a reduction kernel which spawns a thread for each power? (AKA one thread sums up all the x^2 terms, another for x^3, another for x^4, ... x^n)</P></BODY></HTML>Mon, 19 Jul 2010 23:50:42 GMThttps://community.amd.com/t5/archives-discussions/kernel-creation-help/m-p/170541#M14842psath2010-07-19T23:50:42Z