In order to successfully complete this assignment you must do the required reading, watch the provided videos and complete all instructions. The embedded survey form must be entirely filled out and submitted on or before 11:59pm on Thursday March 4. Students must come to class the next day prepared to discuss the material covered in this assignment.
All memory in a computer is stored linearly with each location in memory given a sequential address. I want you to think for a moment to understand how we take a linear list of numbers and turn it into a 2D array:
In the following image you can see the relationship between a linear index and a imposed structure that we give it to represent the row and column of the array. We will set up the problem similar to a checker board, such that each grid has a linear label, an index of row, and an index of column.
To practice, lets make a few simple python functions that convert the incremental label of a grid point to it's corresponding row and column index. For example, in the above ($99 \times 49$) grid the 99-th item is at row = 2 and col = 1 .
✅ DO THIS: Write a function named LabelToIndex
which takes in three arguments (Number of rows, Number of column and the linear index). The function should then return the row and column for that index. The following is a stub function to get you started:
def LabelToIndices(n_row, n_col, lab):
"""function for converting linear label of grid to indices of row and column
Run the function with number of rows, number of columns and the index as input:
Usage:
>>> LabelToIndices(99, 49, 3675)
(75, 0)
"""
return 2,1
help(LabelToIndices)
Help on function LabelToIndices in module __main__: LabelToIndices(n_row, n_col, lab) function for converting linear label of grid to indices of row and column Run the function with number of rows, number of columns and the index as input: Usage: >>> LabelToIndices(99, 49, 3675) (75, 0)
Let's test the LabelToIndices
function for 3675-th grid. Print the index of row and column.
LabelToIndices(99, 49, 3675)
(2, 1)
✅ DO THIS: Using the following stub function as a guide write a function named LabelToIndex
that converts the indices of row and column of a grid to the linear label. For example, the grid at row = 2 and col = 1 is labeled as the 99-th point.
def IndicesToLabel(n_row, n_col, row, col):
"""function for converting row and column indices of a grid to linear label
Usage:
>>> IndicesToLabel(99, 49, 2,1)
99
"""
return 42
IndicesToLabel(99, 49, 2, 1)
42
Each Cuda Kernel is a thread that runs inside of a block that runs inside of a grid. This organization is designed to make thread able to efficiently cooperate with each other. The following picture gives you a mental picture of how threads are organized into blocks which are organized into grids.
Each thread knows it's address based on the following variables:
gridDim.x
, gridDim.y
and gridDim.z
The size of the block grid in the x
, y
and z
directions.blockDim.x
, blockDim.y
and blockDim.z
The size of the block grid in the x
, y
and z
directions.threadIdx.x
, threadIdx.y
and threadIdx.z
. The location of the thread in x,y,z
block coordinates.blockIdx.x
, blockIdx.y
and blockIdx.z
. The location of the block in x,y,z
grid coordinates.Mony programs do not use all three dimensions. For example, consider the following CUDA kernel from the last pre-class assignment which only uses 2D example and assumes the threadIdx.z
and blockIndx.z
are zero:
__global__ void theKernel(float * our_array)
{
//This is array flattening, (Array Width * Y Index + X Index)
int index = (gridDim.x * blockDim.x) * \
(blockIdx.y * blockDim.y + threadIdx.y) + \
(blockIdx.x * blockDim.x + threadIdx.x);
our_array[index] = (float) index;
}
Here is a simpler one from the Vector Add code which only uses one dimension:
__global__ void vecAdd(int *A,int *B,int *C,int N)
{
int i = blockIdx.x * blockDim.x + threadIdx.x;
C[i] = A[i] + B[i];
}
✅ QUESTION: Similar to the above code, how would you calculate a unique index for a three dimensional grid with three dimensional blocks (i.e. take the int index
calculation from above and extend it into three dimensions)?
Put your answer to the above question here.
from IPython.display import YouTubeVideo
YouTubeVideo("usY0643pYs8",width=640,height=360)
✅ QUESTION: What does SM stand for?
Put your answer to the above question here.
In the last pre-class the Kernel (shown above) was lanched using the following code:
dim3 gridSize(2,2,1);
dim3 blockSize(8,8,1);
theKernel <<<gridSize, blockSize>>> (our_array_d);
And the Vector Add example from class used the following code:
int n=10000000;
int block_size=1024;
int block_no = n/block_size;
dim3 dimGrid(block_no,1,1);
dim3 dimBlock(block_size,1,1);
vecAdd<<<block_no,block_size>>>(a_d,b_d,c_d,n);
The following video tries to explain this syntax:
from IPython.display import YouTubeVideo
YouTubeVideo("yNs8B1VnMAA",width=640,height=360)
Consider the following Kernel Call described at the end of the above video:
Kernel <<<dim3(8,4,2), dim3(16,16)>>>(...)
✅ QUESTION: How many blocks are there?
Put your answer to the above question here.
✅ QUESTION: How many Threads per block?
Put your answer to the above question here.
✅ QUESTION: How many total threads?
Put your answer to the above question here.
from IPython.display import YouTubeVideo
YouTubeVideo("cYw7VsyVTe4",width=640,height=360)
✅ QUESTION: What is Branch Divergence and why it is bad?
Put your answer to the above question here.
Please fill out the form that appears when you run the code below. You must completely fill this out in order to receive credits for the assignment!
If you have trouble with the embedded form, please make sure you log on with your MSU google account at googleapps.msu.edu and then click on the direct link above.
✅ Assignment-Specific QUESTION: Are you able to get the index code review working? If not, where did you get stuck?
Put your answer to the above question here
✅ QUESTION: Summarize what you did in this assignment.
Put your answer to the above question here
✅ QUESTION: What questions do you have, if any, about any of the topics discussed in this assignment after working through the jupyter notebook?
Put your answer to the above question here
✅ QUESTION: How well do you feel this assignment helped you to achieve a better understanding of the above mentioned topic(s)?
Put your answer to the above question here
✅ QUESTION: What was the most challenging part of this assignment for you?
Put your answer to the above question here
✅ QUESTION: What was the least challenging part of this assignment for you?
Put your answer to the above question here
✅ QUESTION: What kind of additional questions or support, if any, do you feel you need to have a better understanding of the content in this assignment?
Put your answer to the above question here
✅ QUESTION: Do you have any further questions or comments about this material, or anything else that's going on in class?
Put your answer to the above question here
✅ QUESTION: Approximately how long did this pre-class assignment take?
Put your answer to the above question here
from IPython.display import HTML
HTML(
"""
<iframe
src="https://cmse.msu.edu/cmse401-pc-survey"
width="100%"
height="500px"
frameborder="0"
marginheight="0"
marginwidth="0">
Loading...
</iframe>
"""
)
To get credit for this assignment you must fill out and submit the above survey from on or before the assignment due date.
Written by Dr. Dirk Colbry, Michigan State University
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.