Link to this document's Jupyter Notebook

In order to successfully complete this assignment you need to participate both individually and in groups during class. If you attend class in-person then have one of the instructors check your notebook and sign you out before leaving class on Friday March 5. If you are attending asynchronously, turn in your assignment using D2L no later than _11:59pm on Friday March 5.


In-Class Assignment: CUDA Blocks/Grids/Threadds

image of a greek phalax of fighters. Designed to represent the marching forward of a CUDA WARP

Image from: https://en.wikipedia.org/wiki/Phalanx

Agenda for today's class (70 minutes)

  1. (20 minutes) Pre class Review
  2. (40 minutes) Magma Example
  3. (10 minutes) Start 1D Wave Example

1. Pre class Review

0302--CUDA_Blocks_pre-class-assignment

Visual representation of how a grid is made up of blocks which is then made of of a warp


2. Magma Example

Magma is a large, well-supported software package designed for computations in algebra, number theory, algebraic geometry and algebraic combinatorics. It provides a mathematically rigorous environment for defining and working with structures such as groups, rings, fields, modules, algebras, schemes, curves, graphs, designs, codes and many others. Magma also supports a number of databases designed to aid computational research in those areas of mathematics which are algebraic in nature.

http://magma.maths.usyd.edu.au/magma/

QUESTION: Download a magma example using getexample:

QUESTION: Fix the example to run on a CUDA node properly

QUESTION: Benchmark the example and compare running time in a job betwee k20 and k80 and v100.


3. Start 1D Wave Example

Individually and as a class lets see if we can run the 1D wave example on the GPU and measure the performace

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(int argc, char ** argv) {
    int nx = 500;
    int nt = 100000;
    int i,it;
    double x[nx];
    double y[nx];
    double v[nx];
    double dvdt[nx];
    double dt;
    double dx;
    double max,min;
    double dx2inv;
    double tmax;
    int nxm1;

    max=10.0;
    min=0.0;
    dx = (max-min)/(double)(nx);
    x[0] = min;
    for(i=1;i<nx-1;i++) {
        x[i] = min+(double)i*dx;
    }
    x[nx-1] = max;
    tmax=10.0;
    dt= (tmax-0.0)/(double)(nt);

    for (i=0;i<nx;i++)  {
        y[i] = exp(-(x[i]-5.0)*(x[i]-5.0));
        v[i] = 0.0;
        dvdt[i] = 0.0;
    }

    dx2inv=1.0/(dx*dx);
    nxm1=nx-1;
    for(it=0;it<nt-1;it++) {
        for(i=1;i<nxm1;i++)
            dvdt[i]=(y[i+1]+y[i-1]-2.0*y[i])*(dx2inv);

        for(i=1; i<nxm1; i++)  {
            v[i] = v[i] + dt*dvdt[i];
            y[i] = y[i] + dt*v[i];
        }

    }

    for(i=nx/2-10; i<nx/2+10; i++) {
        printf("%g %g\n",x[i],y[i]);
    }

    return 0;
}

Congratulations, we're done!

If you attend class in-person then have one of the instructors check your notebook and sign you out before leaving class. If you are attending asynchronously, turn in your assignment using D2L.

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.