Question: Add a kernel function to the existing program using CUDA C so that a histogram of the data loaded in loadData() is created. The name

Add a kernel function to the existing program using CUDA C so that a histogram of the data loaded in loadData() is created. The name of the .bin file holding the data is set by a command line argument. The number of intervals in the histogram your kernel will create should also be a command line argument. All intervals need to be the same length. The left endpoint of the first interval needs to be the minimum value in the data and the right endpoint of the last interval needs to be the maximum value. The size of the data needs to be large enough so that not all of the data will fit into 1 block. Hint: loadData() will need some modifications, and you might want to incorporate a reduction algorithm into your kernel for the histogram.

The histogram should be displayed in the following format:

- :

- :

- :

histogram.cu

#include "cuda_runtime.h" #include "device_launch_parameters.h"

#include #include #include

//insert kernel here

void initData(float **vec_h, unsigned size, char* name) { FILE *fw = fopen(name, "wb"); *vec_h = (float*)malloc(size * sizeof(float));

if (*vec_h == NULL) { printf("Unable to allocate host "); }

for (unsigned int i = 0; i < size; i++) { (*vec_h)[i] = (rand() % 100) / 100.00; fprintf(fw, "%0.2f ", (*vec_h)[i]); } fclose(fw); }

void loadData(unsigned size, char* name) { FILE *fw; double vec_k[500];

fw = fopen(name, "rb");

for (unsigned i = 0; i < size; i++) { fscanf(fw, "%lf", &vec_k[i]); printf("%0.2f ", vec_k[i]); }

fclose(fw); }

int main(int argc, char* argv[]) { float *in_h; unsigned in_elements = 500;

char filename[50]; strcpy(filename, argv[1]); char tmp_name[50] = ".bin"; strcat(filename, tmp_name);

initData(&in_h, in_elements, filename); loadData(in_elements, filename); }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!