Step-by-step program optimization [2] OpenACC commands

Source: Internet
Author: User
This has been written for a long time, but has never been concerned about the amount. If you modify this version a little, you only need to add a command to achieve good efficiency. Code: Calpha * A * B + beta * CvoidmySgemm (intm, intn, intk, floatalpha, floatbeta, float * A, float * B, float * C ){

This has been written for a long time, but has never been concerned about the amount. If you modify this version a little, you only need to add a command to achieve good efficiency. Code: // C = alpha * A * B + beta * Cvoid mySgemm (int m, int n, int k, float alpha, float beta, \ float *, float * B, float * C ){

This has been written for a long time, but has never been concerned about the amount. If you modify this version a little, you only need to add a command to achieve good efficiency.

Check the Code:

// C = alpha*A*B + beta*Cvoid mySgemm(int m, int n, int k, float alpha, float beta,\ float *A,  float *B, float *C){int i, j, l;float ab;#pragma acc kernels copy(A[0:m*n],B[0:m*n],C[0:m*n])#pragma acc loop independentfor(j = 0; j < m; j++) {#pragma acc loop independentfor(i = 0 ;i < k ;i++){ab = 0.0f;for(l = 0 ;l < n ;l++){ab += A[j*n+l] * B[l*k+i];}C[j*k+i] = alpha*ab + beta*C[j*k+i];}}}


In this way, we just added several guiding statements. The rest is the compiler's help for us. Your original test program does not need to be changed.

I have talked about how to install and use the HMPP compiler. Compile (after installing CUDA and HMPP) can be compiled using the following command:

$ Hmpp -- codelet-required gcc your_program.c

Run the command and you will find that the speed is quite fast (you must have a CUDA-supported graphics card)

You can write a test program to call this function. Whatever compiler you use, you can find the program provided in this article in your test program, you can use efficient functions.


To improve efficiency, let me modify this Code:

// C = alpha*A*B + beta*Cvoid mySgemm(int m, int n, int k, float alpha, float beta,\ float *A,  float *B, float *C){int i, j, l;float ab;#pragma acc kernels copyin(A[0:m*n],B[0:m*n]) copy(C[0:m*n])#pragma acc loop independentfor(j = 0; j < m; j++) {#pragma acc loop independentfor(i = 0 ;i < k ;i++){ab = 0.0f;for(l = 0 ;l < n ;l++){ab += A[j*n+l] * B[l*k+i];}C[j*k+i] = alpha*ab + beta*C[j*k+i];}}}

In this way, the and B matrices can be transmitted only to the GPU, while the C matrix is transferred to the GPU, and the computation will be re-transmitted back.

In copy (), A [0: m * n] indicates that m * n elements are calculated from the first element. The first element is the starting position, the second value indicates the Data Length.

Let's copy the code and try it !!!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.