CUDA: Getting max value and its index in an array

前端 未结 5 887
旧巷少年郎
旧巷少年郎 2021-01-03 01:57

I have several blocks were each block executes on separate part of an integer array. As an example: block one from array[0] to array[9] and block two from array[10] to array

5条回答
  •  温柔的废话
    2021-01-03 03:00

    One thing to watch out for when doing a max value plus index reduction is that if there is more than one identical valued maximum element in your array, i.e. in your example if there were 2 or more values equal to 56, then the index which is returned would not be unique and possibly be different on every run of the code because the timing of the thread ordering over the GPU is not deterministic.

    To get around this kind of problem you can use a unique ordering index such as threadid + threadsperblock * blockid, or else the element index location if that is unique. Then the max test is along these lines:

    if(a>max_so_far || a==max_so_far && order_a>order_max_so_far)
    { 
        max_so_far = a;
        index_max_so_far = index_a;
        order_max_so_far = order_a;
    }
    

    (index and order can be the same variable, depending on the application.)

提交回复
热议问题