This repository was archived by the owner on Mar 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
Assignment3 #42
Open
amal-majunu
wants to merge
3
commits into
IEEE-NITK:Cuda_C++_SMP
Choose a base branch
from
amal-majunu:Cuda_C++_SMP
base: Cuda_C++_SMP
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Assignment3 #42
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,57 +1,57 @@ | ||
| #include<iostream> | ||
| using namespace std; | ||
|
|
||
| __global__ void Add(float *d_a,float *d_b,float *d_c,int r,int c){ | ||
|
|
||
| int i =blockIdx.x*blockDim.x+threadIdx.x; | ||
| int j =blockIdx.y*blockDim.y+threadIdx.y; | ||
| int k = i+j*c; | ||
| //i is defined for horizontal traversal | ||
| if(i<c && j<r){ | ||
| d_c[k]=d_a[k]+d_b[k]; | ||
| } | ||
| } | ||
|
|
||
|
|
||
| int main() | ||
| { | ||
| int r,c,i,j; | ||
| cout<<"Enter the rows and columns\n"; | ||
| cin>>r>>c; | ||
| float h_a[r][c],h_b[r][c],h_c[r][c]; | ||
| for(i=0;i<r;i++) | ||
| { | ||
| for(j=0;j<c;j++) | ||
| { | ||
| h_a[i][j]=i+j+3; | ||
| h_b[i][j]=i*j; | ||
| } | ||
| } | ||
| float *d_a,*d_b,*d_c; | ||
| cudaMalloc((void**)&d_a, (r*c)*sizeof(float)); | ||
| cudaMalloc((void**)&d_b, (r*c)*sizeof(float)); | ||
| cudaMalloc((void**)&d_c, (r*c)*sizeof(float)); | ||
|
|
||
| cudaMemcpy(d_a, h_a, r*c*sizeof(float), cudaMemcpyHostToDevice); | ||
| cudaMemcpy(d_b, h_b, r*c*sizeof(float), cudaMemcpyHostToDevice); | ||
|
|
||
| dim3 dimBlock(32, 32); | ||
| dim3 dimGrid((int)ceil(1.0*r/dimBlock.x),(int)ceil(1.0*c/dimBlock.y)); | ||
| Add<<<dimGrid,dimBlock>>>(d_a,d_b,d_c,r,c); | ||
| cudaMemcpy(h_c, d_c, (r*c)*sizeof(float), cudaMemcpyDeviceToHost); | ||
|
|
||
| cout<<"Sum of the 2 matrices is:\n"; | ||
| for(i=0;i<r;i++) | ||
| { | ||
| for(j=0;j<c;j++) | ||
| { | ||
| printf("%.2f ",h_c[i][j]); | ||
| } | ||
| cout<<"\n"; | ||
| } | ||
|
|
||
| cudaFree(d_a); | ||
| cudaFree(d_b); | ||
| cudaFree(d_c); | ||
| return 0; | ||
| } | ||
| #include<iostream> | ||
| using namespace std; | ||
| __global__ void Add(float *d_a,float *d_b,float *d_c,int r,int c){ | ||
| int i =blockIdx.x*blockDim.x+threadIdx.x; | ||
| int j =blockIdx.y*blockDim.y+threadIdx.y; | ||
| int k = i+j*c; | ||
| //i is defined for horizontal traversal | ||
| if(i<c && j<r){ | ||
| d_c[k]=d_a[k]+d_b[k]; | ||
| } | ||
| } | ||
| int main() | ||
| { | ||
| int r,c,i,j; | ||
| cout<<"Enter the rows and columns\n"; | ||
| cin>>r>>c; | ||
| float h_a[r][c],h_b[r][c],h_c[r][c]; | ||
| for(i=0;i<r;i++) | ||
| { | ||
| for(j=0;j<c;j++) | ||
| { | ||
| h_a[i][j]=i+j+3; | ||
| h_b[i][j]=i*j; | ||
| } | ||
| } | ||
| float *d_a,*d_b,*d_c; | ||
| cudaMalloc((void**)&d_a, (r*c)*sizeof(float)); | ||
| cudaMalloc((void**)&d_b, (r*c)*sizeof(float)); | ||
| cudaMalloc((void**)&d_c, (r*c)*sizeof(float)); | ||
| cudaMemcpy(d_a, h_a, r*c*sizeof(float), cudaMemcpyHostToDevice); | ||
| cudaMemcpy(d_b, h_b, r*c*sizeof(float), cudaMemcpyHostToDevice); | ||
| dim3 dimBlock(32, 32); | ||
| dim3 dimGrid((int)ceil(1.0*c/dimBlock.x),(int)ceil(1.0*r/dimBlock.y)); | ||
| Add<<<dimGrid,dimBlock>>>(d_a,d_b,d_c,r,c); | ||
| cudaMemcpy(h_c, d_c, (r*c)*sizeof(float), cudaMemcpyDeviceToHost); | ||
| cout<<"Sum of the 2 matrices is:\n"; | ||
| for(i=0;i<r;i++) | ||
| { | ||
| for(j=0;j<c;j++) | ||
| { | ||
| printf("%.2f ",h_c[i][j]); | ||
| } | ||
| cout<<"\n"; | ||
| } | ||
| cudaFree(d_a); | ||
| cudaFree(d_b); | ||
| cudaFree(d_c); | ||
| return 0; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| #include<iostream> | ||
| using namespace std; | ||
|
|
||
| __global__ void Transpose(int *d_a,int max){ | ||
|
|
||
| int i = blockIdx.x*blockDim.x+threadIdx.x; | ||
| int j = blockIdx.y*blockDim.y+threadIdx.y; | ||
| int id1 = i+max*j; | ||
| int id2 = j+max*i; | ||
| __syncthreads(); | ||
|
|
||
| if(i<max && j<max) | ||
| { | ||
| int t = d_a[id1]; | ||
| __syncthreads(); | ||
| d_a[id1]=d_a[id2]; | ||
| __syncthreads(); | ||
| d_a[id2]=t; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| int main() | ||
| { | ||
| int r,c,i,j,max; | ||
| cout<<"Enter the number of rows and columns:\n"; | ||
| cin>>r>>c; | ||
| max=r>c?r:c; | ||
| int h_a[max][max]={0}; | ||
| for(i=0;i<r;i++) | ||
| { | ||
| for(j=0;j<c;j++) | ||
| h_a[i][j]=2*i+j; | ||
| } | ||
| int *d_a; | ||
| cudaMalloc((void**)&d_a, max*max*sizeof(int)); | ||
|
|
||
| cudaMemcpy(d_a, h_a, max*max*sizeof(int), cudaMemcpyHostToDevice); | ||
| dim3 dimBlock(32, 32); | ||
| dim3 dimGrid((int)ceil(1.0*max/dimBlock.x), (int)ceil(1.0*max/dimBlock.y)); | ||
| Transpose<<<dimGrid,dimBlock>>>(d_a,max); | ||
| cudaMemcpy(h_a, d_a, max*max*sizeof(int), cudaMemcpyDeviceToHost); | ||
| cout<<"The transpose matrix is:\n"; | ||
| for(i=0;i<c;i++) | ||
| { | ||
| for(j=0;j<r;j++) | ||
| cout<<h_a[i][j]<<" "; | ||
| cout<<"\n"; | ||
| } | ||
|
|
||
| cudaFree(d_a); | ||
| return 0; | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You need to include another condition in the if statement to check if i<j (or i>j, any one of those). Because you need to operate only on one half of the matrix. You are swapping elements in one half with those in the other half, so only half the threads should perform this operation.