cubla sample-code

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   os   io   for   

cublasSscal 
//Example 1. Application Using C and CUBLAS: 1-based indexing#include <stdlib.h>#include <math.h>#include <cuda_runtime.h>#include "cublas_v2.h"#include <stdio.h>#define M 6#define N 5#define IDX2F(i,j,ld) ((((j)-1)*(ld))+((i)-1))static __inline__ void modify (cublasHandle_t handle, float*m, int ldm, int    n, int p, int q, float alpha, float beta){        cublasSscal (handle, n-p+1, &alpha, &m[IDX2F(p,q,ldm)], ldm);        cublasSscal (handle, ldm-p+1, &beta, &m[IDX2F(p,q,ldm)], 1);}int main (void){    cudaError_t cudaStat;     cublasStatus_t stat;    cublasHandle_t handle;    int i, j;    float* devPtrA;    float* a = 0;    a = (float*)malloc (M * N * sizeof(*a));    if(!a) {        printf("host memory allocation failed");        return EXIT_FAILURE;    }    for(j = 1; j <= N; j++) {        for(i = 1; i <= M; i++) {            a[IDX2F(i,j,M)] = (float)((i-1) * M + j);            printf("%7.0f",a[IDX2F(i,j,M)]);        }printf("\n");    }printf("\n");    cudaStat = cudaMalloc ((void**)&devPtrA, M*N*sizeof(*a));    if(cudaStat != cudaSuccess) {        printf ("device memory allocation failed");        return EXIT_FAILURE;    }    stat = cublasCreate(&handle);    if(stat != CUBLAS_STATUS_SUCCESS) {        printf ("CUBLAS initialization failed\n");        return EXIT_FAILURE;    }    stat = cublasSetMatrix (M, N, sizeof(*a), a, M, devPtrA, M);    if(stat != CUBLAS_STATUS_SUCCESS) {        printf ("data download failed");        cudaFree (devPtrA);        cublasDestroy(handle);        return EXIT_FAILURE;    }    modify (handle, devPtrA, M, N, 2, 3, 16.0f, 12.0f);    stat = cublasGetMatrix (M, N, sizeof(*a), devPtrA, M, a, M);    if(stat != CUBLAS_STATUS_SUCCESS) {        printf("data upload failed");        cudaFree (devPtrA);        cublasDestroy(handle);         return EXIT_FAILURE;    }     cudaFree (devPtrA);    cublasDestroy(handle);    for(j = 1; j <= N; j++) {        for(i = 1; i <= M; i++) {            printf ("%7.0f", a[IDX2F(i,j,M)]);        }        printf ("\n");    }    free(a);    return EXIT_SUCCESS;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.