Coursera MP3

blablabla

// MP 3: Due Sunday, Dec 30, 2012 at 11:59 p.m. PST
#include    <wb.h>

#define wbCheck(stmt) do {                                 \
        cudaError_t err = stmt;                            \
        if (err != cudaSuccess) {                          \
            wbLog(ERROR, "Failed to run stmt ", #stmt);    \
            return -1;                                     \
        }                                                  \
    } while(0)
#define TILE_WIDTH 16
#define TILE_HEIGHT 16
// Compute C = A * B
__global__ void matrixMultiplyShared(float * A, float * B, float * C,
			             int numARows, int numAColumns,
			             int numBRows, int numBColumns,
			             int numCRows, int numCColumns) {
    //@@ Insert code to implement matrix multiplication here
    //@@ You have to use shared memory for this MP
    __shared__ float Ads[TILE_WIDTH][TILE_HEIGHT];
    __shared__ float Bds[TILE_WIDTH][TILE_HEIGHT];

    int bx=blockIdx.x; int by=blockIdx.y;
    int tx=threadIdx.x; int ty=threadIdx.y;

    int Row=by*TILE_HEIGHT+ty;
    int Col=bx*TILE_WIDTH+tx;

    float Pvalue=0;
    for(int m=0;m<ceil(numAColumns/(float)TILE_WIDTH);++m){
        if(Row<numARows&&m*TILE_WIDTH+tx<numAColumns){
            Ads[ty][tx]=A[Row*numAColumns+(m*TILE_WIDTH+tx)];
        }else{
            Ads[ty][tx]=0;
        }
        if(m*TILE_HEIGHT+ty<numBRows&&Col<numBColumns){
            Bds[ty][tx]=B[(m*TILE_HEIGHT+ty)*numBColumns+Col];
        }else{
            Bds[ty][tx]=0;
        }
       
        __syncthreads();

        if(Row<numCRows&&Col<numCColumns){
            for(int k=0;k<TILE_HEIGHT;++k){
                Pvalue+=Ads[ty][k]*Bds[k][tx];
            }
        }
        __syncthreads();
        if(Row<numCRows&&Col<numCColumns)
            C[Row*numCColumns+Col]=Pvalue;
    }

}

int main(int argc, char ** argv) {
    wbArg_t args;
    float * hostA; // The A matrix
    float * hostB; // The B matrix
    float * hostC; // The output C matrix
    float * deviceA;
    float * deviceB;
    float * deviceC;
    int numARows; // number of rows in the matrix A
    int numAColumns; // number of columns in the matrix A
    int numBRows; // number of rows in the matrix B
    int numBColumns; // number of columns in the matrix B
    int numCRows; // number of rows in the matrix C (you have to set this)
    int numCColumns; // number of columns in the matrix C (you have to set this)

    args = wbArg_read(argc, argv);

    wbTime_start(Generic, "Importing data and creating memory on host");
    hostA = (float *) wbImport(wbArg_getInputFile(args, 0), &numARows, &numAColumns);
    hostB = (float *) wbImport(wbArg_getInputFile(args, 1), &numBRows, &numBColumns);
    //@@ Set numCRows and numCColumns
    numCRows = numARows;
    numCColumns = numBColumns;
    //@@ Allocate the hostC matrix
    hostC=(float*)malloc(numCRows*numCColumns*sizeof(float));
    wbTime_stop(Generic, "Importing data and creating memory on host");

    wbLog(TRACE, "The dimensions of A are ", numARows, " x ", numAColumns);
    wbLog(TRACE, "The dimensions of B are ", numBRows, " x ", numBColumns);

    wbTime_start(GPU, "Allocating GPU memory.");
    //@@ Allocate GPU memory here
    cudaMalloc((void**)&deviceA,numARows*numAColumns*sizeof(float));
    cudaMalloc((void**)&deviceB,numBRows*numBColumns*sizeof(float));
    cudaMalloc((void**)&deviceC,numCRows*numCColumns*sizeof(float));
    wbTime_stop(GPU, "Allocating GPU memory.");

    wbTime_start(GPU, "Copying input memory to the GPU.");
    //@@ Copy memory to the GPU here
    cudaMemcpy(deviceA,hostA,numARows*numAColumns*sizeof(float),cudaMemcpyHostToDevice);
    cudaMemcpy(deviceB,hostB,numBRows*numBColumns*sizeof(float),cudaMemcpyHostToDevice);
    wbTime_stop(GPU, "Copying input memory to the GPU.");
    
    //@@ Initialize the grid and block dimensions here
    dim3 dimBlock(TILE_HEIGHT,TILE_WIDTH,1);
    dim3 dimGrid(ceil(numCColumns/(float)TILE_HEIGHT),ceil(numCRows/(float)TILE_WIDTH),1);
    wbTime_start(Compute, "Performing CUDA computation");
    //@@ Launch the GPU Kernel here
    matrixMultiplyShared<<<dimGrid,dimBlock>>>(deviceA,deviceB,deviceC,numARows,numAColumns,numBRows,numBColumns,numCRows,numCColumns);
    cudaThreadSynchronize();
    wbTime_stop(Compute, "Performing CUDA computation");
    
    wbTime_start(Copy, "Copying output memory to the CPU");
    //@@ Copy the GPU memory back to the CPU here
    cudaMemcpy(hostC,deviceC,numCRows*numCColumns*sizeof(float),cudaMemcpyDeviceToHost);
    wbTime_stop(Copy, "Copying output memory to the CPU");

    wbTime_start(GPU, "Freeing GPU Memory");
    //@@ Free the GPU memory here
    cudaFree(deviceA);cudaFree(deviceB);cudaFree(deviceC);
    wbTime_stop(GPU, "Freeing GPU Memory");

    wbSolution(args, hostC, numCRows, numCColumns);

    free(hostA);
    free(hostB);
    free(hostC);

    return 0;
}

转载于:https://my.oschina.net/xinyi/blog/98838

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Coursera 是一个知名的在线学习平台,它有一些明显的优点和一些潜在的缺点。以下是一些常见的优缺点: 优点: 1. 多样化的课程选择:Coursera 提供了广泛的课程选择,涵盖了各种学科和领域,包括计算机科学、人文学科、商业管理等。无论你是想学习新知识还是提升职业技能,都能找到适合的课程。 2. 来自顶尖大学和机构的课程:Coursera 合作伙伴包括世界各地的顶尖大学和知名机构,如斯坦福大学、耶鲁大学、谷歌等。这意味着你可以通过 Coursera 学习到高质量的课程内容。 3. 灵活的学习体验:Coursera 的课程采用在线视频、练习题、论坛讨论等多种学习方式,让学习过程更加灵活和自主。你可以根据自己的节奏和时间安排来学习课程。 缺点: 1. 部分课程需要付费:尽管 Coursera 提供了许多免费课程,但一些高级课程和专业证书项目可能需要付费。这可能对一些学生来说是一个限制因素。 2. 缺乏个性化指导:由于 Coursera 的课程规模庞大,个别学生可能会感到缺乏个性化的指导和反馈。这对于一些学习者来说可能是一个挑战。 3. 课程质量参差不齐:虽然 Coursera 合作伙伴中有许多顶尖大学和机构,但并不是所有课程的质量都一样。有些课程可能缺乏深度或实际应用,需要学生自己判断和选择。 综上所述,Coursera 是一个提供广泛课程选择和灵活学习体验的在线学习平台,但也需要学生自己判断课程质量和付费情况。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值