|
| 1 | +/* Copyright (c) 1993-2015, NVIDIA CORPORATION. All rights reserved. |
| 2 | + * |
| 3 | + * Redistribution and use in source and binary forms, with or without |
| 4 | + * modification, are permitted provided that the following conditions |
| 5 | + * are met: |
| 6 | + * * Redistributions of source code must retain the above copyright |
| 7 | + * notice, this list of conditions and the following disclaimer. |
| 8 | + * * Redistributions in binary form must reproduce the above copyright |
| 9 | + * notice, this list of conditions and the following disclaimer in the |
| 10 | + * documentation and/or other materials provided with the distribution. |
| 11 | + * * Neither the name of NVIDIA CORPORATION nor the names of its |
| 12 | + * contributors may be used to endorse or promote products derived |
| 13 | + * from this software without specific prior written permission. |
| 14 | + * |
| 15 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY |
| 16 | + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 17 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 18 | + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 19 | + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 20 | + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 22 | + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 23 | + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 25 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | + */ |
| 27 | +#include "particle.h" |
| 28 | +#include <stdlib.h> |
| 29 | +#include <stdio.h> |
| 30 | + |
| 31 | +__global__ void advanceParticles(float dt, particle * pArray, int nParticles) |
| 32 | +{ |
| 33 | + int idx = threadIdx.x + blockIdx.x*blockDim.x; |
| 34 | + if(idx < nParticles) |
| 35 | + { |
| 36 | + pArray[idx].advance(dt); |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +int main(int argc, char ** argv) |
| 41 | +{ |
| 42 | + cudaError_t error; |
| 43 | + int n = 1000000; |
| 44 | + if(argc > 1) { n = atoi(argv[1]);} // Number of particles |
| 45 | + if(argc > 2) { srand(atoi(argv[2])); } // Random seed |
| 46 | + |
| 47 | + error = cudaGetLastError(); |
| 48 | + if (error != cudaSuccess) |
| 49 | + { |
| 50 | + printf("0 %s\n",cudaGetErrorString(error)); |
| 51 | + exit(1); |
| 52 | + } |
| 53 | + |
| 54 | + particle * pArray = new particle[n]; |
| 55 | + particle * devPArray = NULL; |
| 56 | + cudaMalloc(&devPArray, n*sizeof(particle)); |
| 57 | + cudaDeviceSynchronize(); error = cudaGetLastError(); |
| 58 | + if (error != cudaSuccess) |
| 59 | + { |
| 60 | + printf("1 %s\n",cudaGetErrorString(error)); |
| 61 | + exit(1); |
| 62 | + } |
| 63 | + |
| 64 | + cudaMemcpy(devPArray, pArray, n*sizeof(particle), cudaMemcpyHostToDevice); |
| 65 | + cudaDeviceSynchronize(); error = cudaGetLastError(); |
| 66 | + if (error != cudaSuccess) |
| 67 | + { |
| 68 | + printf("2 %s\n",cudaGetErrorString(error)); |
| 69 | + exit(1); |
| 70 | + } |
| 71 | + |
| 72 | + for(int i=0; i<100; i++) |
| 73 | + { |
| 74 | + float dt = (float)rand()/(float) RAND_MAX; // Random distance each step |
| 75 | + advanceParticles<<< 1 + n/256, 256>>>(dt, devPArray, n); |
| 76 | + error = cudaGetLastError(); |
| 77 | + if (error != cudaSuccess) |
| 78 | + { |
| 79 | + printf("3 %s\n",cudaGetErrorString(error)); |
| 80 | + exit(1); |
| 81 | + } |
| 82 | + |
| 83 | + cudaDeviceSynchronize(); |
| 84 | + } |
| 85 | + cudaMemcpy(pArray, devPArray, n*sizeof(particle), cudaMemcpyDeviceToHost); |
| 86 | + |
| 87 | + v3 totalDistance(0,0,0); |
| 88 | + v3 temp; |
| 89 | + for(int i=0; i<n; i++) |
| 90 | + { |
| 91 | + temp = pArray[i].getTotalDistance(); |
| 92 | + totalDistance.x += temp.x; |
| 93 | + totalDistance.y += temp.y; |
| 94 | + totalDistance.z += temp.z; |
| 95 | + } |
| 96 | + float avgX = totalDistance.x /(float)n; |
| 97 | + float avgY = totalDistance.y /(float)n; |
| 98 | + float avgZ = totalDistance.z /(float)n; |
| 99 | + float avgNorm = sqrt(avgX*avgX + avgY*avgY + avgZ*avgZ); |
| 100 | + printf( "Moved %d particles 100 steps. Average distance traveled is |(%f, %f, %f)| = %f\n", |
| 101 | + n, avgX, avgY, avgZ, avgNorm); |
| 102 | + return 0; |
| 103 | +} |
0 commit comments