source: trunk/ADOL-C/examples/additional_examples/cuda/traceless_cuda.cu @ 406

Last change on this file since 406 was 406, checked in by kulshres, 9 years ago

Merge branch 'cuda' of 'gitclone'

Signed-off-by: Kshitij Kulshreshtha <kshitij@…>

The following commits were merged:

commit b62001306013a3cff8685cd41355db4bbcdbdfe1
Author: Kshitij Kulshreshtha <kshitij@…>
Date: Tue Feb 12 10:46:48 2013 +0100

Add examples to build system, copyrights and multi-include protection

Signed-off-by: Kshitij Kulshreshtha <kshitij@…>

commit ce0f1c68301992d307136ac114af10b0a05e8915
Author: Alina Koniaeva <alinak@…>
Date: Thu Jul 12 16:19:38 2012 +0200

sqrt and log function changed in the header file adoublecuda.h

Signed-off-by: Alina Koniaeva <alinak@…>

commit eed8665516916084e3b7dd0a783938f814d26391
Author: Alina Koniaeva <alinak@…>
Date: Thu Jul 12 16:18:34 2012 +0200

Examples for the use of ADOL-C with Cuda

Signed-off-by: Alina Koniaeva <alinak@…>

commit c1dff45b95768c536366cd688281488721279c3d
Author: Alina Koniaeva <alinak@…>
Date: Thu Jul 12 16:13:34 2012 +0200

Documentation for the use of ADOL-C with Cuda

Signed-off-by: Alina Koniaeva <alinak@…>

commit 7d3ffd136d5785162beeb7e7f1063e50d236f001
Author: Alina Koniaeva <alinak@…>
Date: Fri Jun 1 16:52:05 2012 +0200

Example for the use of ADOL-C with Cuda

Signed-off-by: Alina Koniaeva <alinak@…>

commit e59c0d04e59e01e3c498f6e3e60aa1371ed0fbc2
Author: Alina Koniaeva <alinak@…>
Date: Fri Jun 1 16:51:20 2012 +0200

Headerfile adoublecuda.h included in makefile

Signed-off-by: Alina Koniaeva <alinak@…>

commit 3a81a3ae62be40673cd6d915e5aa56e57996e918
Author: Alina Koniaeva <alinak@…>
Date: Fri Jun 1 16:50:37 2012 +0200

Headerfile for the use of ADOL-C with Cuda

Signed-off-by: Alina Koniaeva <alinak@…>

File size: 3.1 KB
Line 
1/*----------------------------------------------------------------------------
2 ADOL-C -- Automatic Differentiation by Overloading in C++
3 File:     traceless_cuda.cu
4 Revision: $Id$
5 Contents: computation of coordinate transform,
6           cuda example described in the manual
7
8 Copyright (c) Andrea Walther, Alina Koniaeva
9 
10 This file is part of ADOL-C. This software is provided as open source.
11 Any use, reproduction, or distribution of the software constitutes
12 recipient's acceptance of the terms of the accompanying license file.
13 
14---------------------------------------------------------------------------*/
15
16/****************************************************************************/
17/*                                                                 INCLUDES */
18
19#include "adoublecuda.h"
20#include <cuda.h>
21#include <iostream>
22
23using namespace std;
24
25__global__ void kernel(double* inx, double* outy, double* outderiv) {
26
27    const int index = threadIdx.x ;
28    const int index1 = threadIdx.y;
29    const int index2 = blockIdx.x;
30    const int dim = blockDim.x*blockDim.y;
31    const int index3 = blockDim.x;
32
33    // Declare dependent and independent variables as adoubles
34    adtlc::adouble y[3];
35    adtlc::adouble x[3];
36
37    // Read out point for function evaluation
38    for(int i=0;i<3;i++)
39        x[i]=inx[index2*dim+index*3+i];
40
41    // Set direction for calculation of derivatives
42    x[index1].setADValue(1);
43
44    // Function evaluation
45    y[0] = sqrt(x[0]*x[0]+x[1]*x[1]+x[2]*x[2]);
46    y[1] = atan(sqrt(x[0]*x[0]+x[1]*x[1])/x[2]);
47    y[2] = atan(x[1]/x[0]);
48
49   for(int i=0; i<3; i++) outy[(index2*index3+index)*3+i]=y[i].getValue();
50   for(int i=0; i<3; i++) outderiv[(index2*dim+index*3+index1)*3+i]=y[i].getADValue();
51       
52}
53 
54cudaError_t kernellaunch(double* inx, double* outy, double* outderiv, int n) {
55    // Create 16 blocks
56    int Blocks=16;
57    // Two dimensional (M/Blocks) x 3 blocks
58    dim3 threadsPerBlock(n/Blocks,3);
59   
60    // Call kernel function with 16 blocks with (M/Blocks) x 3 threads per block
61    kernel <<< Blocks, threadsPerBlock >>>( inx, outy, outderiv);
62    cudaError_t cudaErr = cudaGetLastError();
63
64    return cudaErr;
65}
66
67
68int main(){
69
70    int M=1024;
71    double* deriv = new double[9*M];
72    double* y = new double[3*M];
73    double* x = new double[3*M];
74
75    // Initialize x_i
76    for(int k=0; k < M; k++){
77     for (int i=0; i<3; ++i)         
78        x[k*3+i] =i + 1/(k+1);}
79
80    // Allocate array for independent and dependent variables and Jacobian matrices on GPU
81    double * devx;
82    cudaMalloc((void**)&devx, 3*M*sizeof(double));
83
84    double * devy;
85    cudaMalloc((void**)&devy, 3*M*sizeof(double));
86
87    double * devderiv;
88    cudaMalloc((void**)&devderiv, 3*3*M*sizeof(double));
89
90    // Copy values of independent variables from host to GPU
91    cudaMemcpy(devx, x, sizeof(double)*3*M, cudaMemcpyHostToDevice);
92 
93    // Call function to specify amount of blocks and threads to be used
94    kernellaunch(devx, devy, devderiv,M);
95 
96    // Copy values of dependent variables and Jacobian matrices from GPU to host
97    cudaMemcpy(y, devy, sizeof(double)*3*M, cudaMemcpyDeviceToHost);
98
99    cudaMemcpy(deriv, devderiv, sizeof(double)*M*3*3, cudaMemcpyDeviceToHost);
100}
Note: See TracBrowser for help on using the repository browser.