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.
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]);
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?
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)
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).
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. :)
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]
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.
@@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!
@@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).
nice tutorial
Awesome! Thank you so much for those tutorials!!!
Thanks. I'm glad you found it useful.
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.
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]);
Yep good catch!
Thank you was helpful:)
Thanks for the video!!
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?
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)
Thanks for sharing this.
Glad you found it useful!
So you are using a grid size of 256 blocks and each block has 256 threads => 65.536 threads.
hey, thanks for the video, can i use this code for 3ds max to make a better rendering ?
Thanks. Yes this is completely open souce so use it however you want.
Sorry I wanted to say whether one can calculate the same minimum value, if one instead of fmaxf, fminf will use
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).
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. :)
true. It doesnt work. Would you please clean your sh*t from yt and avoid people wasting time on some code that doesnt work.
Hello, can you help please to find the index of the maximum value in an array?
Thank you
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]
that should be int *maxIndex in the kernel declaration
James Sandham, Thank you very much!
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 ?
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.
@@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!
@@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).
See cplusplus.com/reference/cstdlib/srand/ as an example
@@jamessandham1941 thank you man I understand now you are the best !!!
Real man implement bubble sort and then get the last element of the array
can you post a github for this?
+Robin Huang I have added a link in the description to my bitbucket account where you can look at the files.