Algorithms in CUDA: finding max value in an array

Поделиться
HTML-код
  • Опубликовано: 1 дек 2024

Комментарии • 34

  • @sukruciris
    @sukruciris 3 месяца назад

    nice tutorial

  • @rendermanpro
    @rendermanpro 7 лет назад +2

    Awesome! Thank you so much for those tutorials!!!

  • @IsraelAmar1969
    @IsraelAmar1969 11 месяцев назад

    Hi James. How can I change kernel & main code to handle 2d array and find max value per each column in that array? I have 50 columns, each has 5000 consecutive float elements in memory.

  • @cripplededu
    @cripplededu 7 лет назад +1

    awesome video i found it quite useful, there are two minors errors, first when you initialize the max (d_max) to 0 it will return this zero if all the numbers of the array are less than 0, it should be any number of the array, the second is on the kernel sould also initialize temp with a value in the array (temp = array[0]);

  • @sunilwaghamare18
    @sunilwaghamare18 3 года назад

    Thank you was helpful:)

  • @manvisharma3997
    @manvisharma3997 5 лет назад

    Thanks for the video!!

  • @rbnstmar
    @rbnstmar 3 года назад

    Hi James, great video, and really helpful to me. What should I do to obtain not only the maximum number of an array of numbers, but also the index of the maximum number in the array?

  • @davidecangiano9340
    @davidecangiano9340 Год назад

    I try to do the min version of this code, but when the array of floats has each element greater than -1.0, the minimum value that kernel returns is always -1.0. I try to assign the macro FLT_MAX to variable "temp" in the kernel, but there's another problem: this time the minimum value is 0.0, but in the input array there's not this value. Please, can anyone help me to fix these problems? (I am very desperate)

  • @amitpatelpatel144
    @amitpatelpatel144 7 лет назад

    Thanks for sharing this.

  • @grigorecosmin
    @grigorecosmin 3 года назад

    So you are using a grid size of 256 blocks and each block has 256 threads => 65.536 threads.

  • @Haydu_lion
    @Haydu_lion 6 лет назад

    hey, thanks for the video, can i use this code for 3ds max to make a better rendering ?

    • @jamessandham1941
      @jamessandham1941  6 лет назад

      Thanks. Yes this is completely open souce so use it however you want.

  • @aminladal
    @aminladal 7 лет назад

    Sorry I wanted to say whether one can calculate the same minimum value, if one instead of fmaxf, fminf will use

    • @jamessandham1941
      @jamessandham1941  7 лет назад

      You should be able to do the same thing with fminf to find the minimum. You will have to change the initial temp though. For example if your array was [3,6,5,8,7,2,10,9] than with temp=0 (as currently written) the kernel will say that 0 is the minimum. If you changed temp=10 (or higher in this example), than you should get the correct result I think (I havn't tested this though).

  • @hammerhead83
    @hammerhead83 6 лет назад +3

    Your method isn't working. Try a small array size (e.g. 10 values) and write to console array values. Compare results by human-brain comparison. This method calculates number of executed threads only. :)

    • @lowlifeuk999
      @lowlifeuk999 4 года назад

      true. It doesnt work. Would you please clean your sh*t from yt and avoid people wasting time on some code that doesnt work.

  • @tp023988
    @tp023988 7 лет назад

    Hello, can you help please to find the index of the maximum value in an array?
    Thank you

    • @jamessandham1941
      @jamessandham1941  7 лет назад

      I think that requires only a few minor modifications of the existing code that I describe in the video (and which you can download from my bitbucket account listed in the description). In particular in think you would just change the kernel to something like:
      __global__ void find_maximum_kernel(float *array, float *max, int* maxIndex, int *mutex, unsigned int n)
      {
      unsigned int index = threadIdx.x + blockIdx.x*blockDim.x;
      unsigned int stride = gridDim.x*blockDim.x;
      unsigned int offset = 0; __shared__ float cache[256];
      __shared__ int indexCache[256];
      float temp = -1.0;
      int tempIndex = 0;
      while(index + offset < n){
      if(temp < array[index + offset]){
      temp = array[index+offset]);
      tempIndex = index+offset;
      } offset += stride;
      } cache[threadIdx.x] = temp;
      indexCache[threadIdx.x] = tempIndex;
      __syncthreads();
      // reduction
      unsigned int i = blockDim.x/2;
      while(i != 0){
      if(threadIdx.x < i){
      if(cache[threadIdx.x]

    • @jamessandham1941
      @jamessandham1941  7 лет назад

      that should be int *maxIndex in the kernel declaration

    • @tp023988
      @tp023988 7 лет назад

      James Sandham, Thank you very much!

  • @rdavid5904
    @rdavid5904 Год назад

    Hey man sorry, but i get the same result over and over, why do I get the same max number every time I run the program ?

    • @jamessandham1941
      @jamessandham1941  Год назад

      What is it that you expect it to produce? The goal was to compute the max value in an array. You should expect to get the same value every time. Anything else would be a bug. Now if you use different arrays each time you run then you would expect different results.

    • @rdavid5904
      @rdavid5904 Год назад

      @@jamessandham1941 yeah I understand but we are generating the array using a random function. So the numbers should also be different everytime? So the max value from that random array should be different? Sorry I don't really understand very well what the program is doing, I am trying to learn thank you!!! Sorry for bothering!

    • @jamessandham1941
      @jamessandham1941  Год назад

      @@rdavid5904 We are generating an array with a random function with the same seed. Generally, random number generators take a seed. This seed will result in the random number generator generating the same "random" numbers every time (this is important because otherwise debugging problems would be a nightmare if you got different numbers run to run). If you want to generate different random numbers every time you run the program, you need to use a different seed in the random number generator (by for example using the time of day as a seed).

    • @jamessandham1941
      @jamessandham1941  Год назад

      See cplusplus.com/reference/cstdlib/srand/ as an example

    • @rdavid5904
      @rdavid5904 Год назад

      @@jamessandham1941 thank you man I understand now you are the best !!!

  • @jussehwagner3166
    @jussehwagner3166 3 года назад

    Real man implement bubble sort and then get the last element of the array

  • @robinhuang3129
    @robinhuang3129 8 лет назад

    can you post a github for this?

    • @jamessandham1941
      @jamessandham1941  8 лет назад +1

      +Robin Huang I have added a link in the description to my bitbucket account where you can look at the files.