aboutsummaryrefslogtreecommitdiff
path: root/benchmarks/CUDA/CP/benchmarks/cp/src/cuda_base/main.cu
blob: 00b01556e3a073659c987c3493afe3765d27919c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/***************************************************************************
 *cr
 *cr            (C) Copyright 2007 The Board of Trustees of the
 *cr                        University of Illinois
 *cr                         All Rights Reserved
 *cr
 ***************************************************************************/
/*
 * CUDA accelerated coulombic potential grid test code
 *   John E. Stone <[email protected]>
 *   http://www.ks.uiuc.edu/~johns/
 *
 * Coulombic potential grid calculation microbenchmark based on the time
 * consuming portions of the 'cionize' ion placement tool.
 */

#include <parboil.h>
#include <stdio.h>
#include <stdlib.h>

#include "cuenergy.h"

/* initatoms()
 * Store a pseudorandom arrangement of point charges in *atombuf.
 */
static int
initatoms(float **atombuf, int count, dim3 volsize, float gridspacing) {
  dim3 size;
  int i;
  float *atoms;

  srand(54321);			// Ensure that atom placement is repeatable

  atoms = (float *) malloc(count * 4 * sizeof(float));
  *atombuf = atoms;

  // compute grid dimensions in angstroms
  size.x = gridspacing * volsize.x;
  size.y = gridspacing * volsize.y;
  size.z = gridspacing * volsize.z;

  for (i=0; i<count; i++) {
    int addr = i * 4;
    atoms[addr    ] = (rand() / (float) RAND_MAX) * size.x; 
    atoms[addr + 1] = (rand() / (float) RAND_MAX) * size.y; 
    atoms[addr + 2] = (rand() / (float) RAND_MAX) * size.z; 
    atoms[addr + 3] = ((rand() / (float) RAND_MAX) * 2.0) - 1.0;  // charge
  }  

  return 0;
}

/* writeenergy()
 * Write part of the energy array to an output file for verification.
 */
static int
writeenergy(char *filename, float *energy, dim3 volsize)
{
  FILE *outfile;
  int x, y;

  outfile = fopen(filename, "w");
  if (outfile == NULL) {
    fputs("Cannot open output file\n", stderr);
    return -1;
    }

  /* Print the execution parameters */
  fprintf(outfile, "%d %d %d %d\n", volsize.x, volsize.y, volsize.z, ATOMCOUNT);

  /* Print a checksum */
  {
    double sum = 0.0;

    for (y = 0; y < volsize.y; y++) {
      for (x = 0; x < volsize.x; x++) {
        double t = energy[y*volsize.x+x];
        t = fmax(-20.0, fmin(20.0, t));
    	sum += t;
      }
    }
    fprintf(outfile, "%.4g\n", sum);
  }
  
  /* Print several rows of the computed data */
  for (y = 0; y < 17; y++) {
    for (x = 0; x < volsize.x; x++) {
      int addr = y * volsize.x + x;
      fprintf(outfile, "%.4g ", energy[addr]);
    }
    fprintf(outfile, "\n");
  }

  fclose(outfile);

  return 0;
}

int main(int argc, char** argv) {
  struct pb_TimerSet timers;
  struct pb_Parameters *parameters;

  float *energy = NULL;		// Output of device calculation
  float *atoms = NULL;
  dim3 volsize, Gsz, Bsz;

  // int final_iteration_count;

  // number of atoms to simulate
  int atomcount = ATOMCOUNT;

  // voxel spacing
  const float gridspacing = 0.1;

  // Size of buffer on GPU
  int volmemsz;

  printf("CUDA accelerated coulombic potential microbenchmark\n");
  printf("Original version by John E. Stone <[email protected]>\n");
  printf("This version maintained by Chris Rodrigues\n");

  parameters = pb_ReadParameters(&argc, argv);
  if (!parameters)
    return -1;

  if (parameters->inpFiles[0]) {
    fputs("No input files expected\n", stderr);
    return -1;
  }

  pb_InitializeTimerSet(&timers);
  pb_SwitchToTimer(&timers, pb_TimerID_COMPUTE);

  // setup energy grid size
  volsize.x = VOLSIZEX;
  volsize.y = VOLSIZEY;
  volsize.z = 1;

  // setup CUDA grid and block sizes
  Bsz.x = BLOCKSIZEX;		// each thread does multiple Xs
  Bsz.y = BLOCKSIZEY;
  Bsz.z = 1;
  Gsz.x = volsize.x / (Bsz.x * UNROLLX); // each thread does multiple Xs
  Gsz.y = volsize.y / Bsz.y; 
  Gsz.z = volsize.z / Bsz.z; 

#if 0
  printf("Grid size: %d x %d x %d\n", volsize.x, volsize.y, volsize.z);
  printf("Running kernel(atoms:%d, gridspacing %g, z %d)\n", atomcount, gridspacing, 0);
#endif

  // allocate and initialize atom coordinates and charges
  if (initatoms(&atoms, atomcount, volsize, gridspacing))
    return -1;

  // allocate and initialize the GPU output array
  volmemsz = sizeof(float) * volsize.x * volsize.y * volsize.z;

  // Main computation
  {
    float *d_output = NULL;	// Output on device
    int iterations=0;
    int atomstart;

    pb_SwitchToTimer(&timers, pb_TimerID_COPY);
    cudaMalloc((void**)&d_output, volmemsz);
    CUERR // check and clear any existing errors
    cudaMemset(d_output, 0, volmemsz);
    CUERR // check and clear any existing errors
    pb_SwitchToTimer(&timers, pb_TimerID_COMPUTE);

    for (atomstart=0; atomstart<atomcount; atomstart+=MAXATOMS) {   
      int atomsremaining = atomcount - atomstart;
      int runatoms = (atomsremaining > MAXATOMS) ? MAXATOMS : atomsremaining;
      iterations++;

      // copy the atoms to the GPU
      pb_SwitchToTimer(&timers, pb_TimerID_COPY);
      if (copyatomstoconstbuf(atoms + 4*atomstart, runatoms, 0*gridspacing)) 
	return -1;

      if (parameters->synchronizeGpu) cudaThreadSynchronize();
      pb_SwitchToTimer(&timers, pb_TimerID_GPU);
 
      // RUN the kernel...
      pb_StartTimer(&timers.gpu);
      cenergy<<<Gsz, Bsz, 0>>>(runatoms, 0.1, d_output);
      CUERR // check and clear any existing errors

      if (parameters->synchronizeGpu) cudaThreadSynchronize();
      pb_SwitchToTimer(&timers, pb_TimerID_COMPUTE);

      // final_iteration_count = iterations;
    }
#if 0
    printf("Done\n");
#endif

    // Copy the GPU output data back to the host and use/store it..
    energy = (float *) malloc(volmemsz);
    pb_SwitchToTimer(&timers, pb_TimerID_COPY);
    cudaMemcpy(energy, d_output, volmemsz,  cudaMemcpyDeviceToHost);
    CUERR // check and clear any existing errors

    cudaFree(d_output);

    pb_SwitchToTimer(&timers, pb_TimerID_COMPUTE);
  }

  /* Print a subset of the results to a file */
  if (parameters->outFile) {
    pb_SwitchToTimer(&timers, pb_TimerID_IO);
    if (writeenergy(parameters->outFile, energy, volsize) == -1)
      return -1;
    pb_SwitchToTimer(&timers, pb_TimerID_COMPUTE);
  }

  free(atoms);
  free(energy);

  pb_SwitchToTimer(&timers, pb_TimerID_NONE);

  pb_PrintTimerSet(&timers);
  pb_FreeParameters(parameters);

  return 0;
}