Link to this document's Jupyter Notebook

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.


Pre-Class Assignment: Scheduling CUDA Jobs

Goals for today's pre-class assignment

  1. 2D Array Indexing (a review)
  2. Indexing a Kernel using Blocks and Grids
  3. Launching a Kernel
  4. Branch Divergence
  5. Assignment wrap up

1. 2D Array Indexing (a review)

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.

Visual relationship between linear index and a 2D index

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:

Let's test the LabelToIndices function for 3675-th grid. Print the index of row and column.

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.


2. Indexing a Kernel using Blocks and Grids

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:

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.

QUESTION: What does SM stand for?

Put your answer to the above question here.


3. Launching a Kernel

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:

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.


4. Branch Divergence

QUESTION: What is Branch Divergence and why it is bad?

Put your answer to the above question here.


5. Assignment wrap up

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!

Direct Link to Google Form

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


Congratulations, we're done!

To get credit for this assignment you must fill out and submit the above survey from on or before the assignment due date.

Course Resources:

Written by Dr. Dirk Colbry, Michigan State University Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.