Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Standard view
Full view
of .
Look up keyword
Like this
0 of .
Results for:
No results containing your search query
P. 1
Matrix Mul Blocks.cu

Matrix Mul Blocks.cu

Ratings: (0)|Views: 19|Likes:
Published by Farhan Ahmad
CUDA C program for multilying two matrices using more than one blocks of threads.
CUDA C program for multilying two matrices using more than one blocks of threads.

More info:

Published by: Farhan Ahmad on Oct 04, 2010
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as TXT, PDF, TXT or read online from Scribd
See more
See less





//Matrix multiplication using multiple blocks#include<stdio.h>#include<conio.h>#include<cuda.h>//Kernel funtion __global__ void matrix_mul_blocks(float *first, float *second,float *result,inttile_width,int width1,int width2){float a,b,sum;int idx = threadIdx.x;int idy = threadIdx.y;int bx = blockIdx.x;int by = blockIdx.y;int k ,uidx , uidy ;uidx = bx*tile_width + idx;uidy = by*tile_width + idy;sum =0;for(k=0 ; k<width1 ;k++){a = first[uidy*width1+k];b = second[uidx + k*width2];sum = sum + (a*b);}result[uidy*width2 + uidx] = sum;}void main(){float *a_h, *b_h, *c_h, *a_d, *b_d, *c_d;int row1, col1, row2, col2 ;int tile_width , width1 , width2;char option;//acquiring the sizeprintf("Enter the size of the first matrix(row col)>>");scanf("%d %d",&row1,&col1);printf("Enter the size of the second matrix(row col)>>");scanf("%d %d",&row2,&col2);//memory allocation on hosta_h = (float*)malloc(sizeof(float)*row1*col1);b_h = (float*)malloc(sizeof(float)*row2*col2);c_h = (float*)malloc(sizeof(float)*row1*col2);//memory allocation on devicecudaMalloc((void**)&a_d,sizeof(float)*row1*col1);cudaMalloc((void**)&b_d,sizeof(float)*row2*col2);cudaMalloc((void**)&c_d,sizeof(float)*row1*col2);//getting the data on hostprintf("Want to enter automatically(Y/N)>>");
scanf(" %c",&option);if(option== 'Y'){for(int i =0;i<row1;i++){for(int j =0;j<col1;j++){a_h[i*col1 +j] = i+j;}}for(int i=0;i<row2;i++){for(int j=0;j<col2;j++){b_h[i*col2 +j] = i*j;}}}else if(option == 'N'){printf("Enter the data for the first matrix\n");for(int i =0;i<row1;i++){for(int j=0;j<col1;j++){printf("Enter the element a_h[%d][%d]>>",i,j);scanf("%f",&a_h[i*col1+j]);}}printf("Enter the data for the second matrix\n");for(int i =0;i<row2;i++){for(int j=0;j<col2;j++){printf("Enter the element b_h[%d][%d]>>",i,j);scanf("%f",&b_h[i*col2+j]);}}}//printing the data enteredfor(int i =0;i<row1;i++){for(int j=0;j<col1;j++){printf("%f\t",a_h[i*col1+j]);}printf("\n");}printf("\n");for(int i =0;i<row2;i++){for(int j=0;j<col2;j++){printf("%f\t",b_h[i*col2+j]);}printf("\n");

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->