-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathbude.c
251 lines (220 loc) · 6.76 KB
/
bude.c
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#include <float.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <omp.h>
#include "shared.h"
#define MAX_PLATFORMS 8
#define MAX_DEVICES 32
#define MAX_INFO_STRING 256
#define DATA_DIR "../data/bm1"
#define FILE_LIGAND "/ligand.in"
#define FILE_PROTEIN "/protein.in"
#define FILE_FORCEFIELD "/forcefield.in"
#define FILE_POSES "/poses.in"
#define FILE_REF_ENERGIES "/ref_energies.out"
#define REF_NPOSES 65536
// Energy evaluation parameters
#define CNSTNT 45.0f
#define HBTYPE_F 70
#define HBTYPE_E 69
#define HARDNESS 38.0f
#define NPNPDIST 5.5f
#define NPPDIST 1.0f
void loadParameters(int argc, char *argv[]);
void freeParameters();
void printTimings(double start, double end, double poses_per_wi);
void checkError(int err, const char *op);
void runCUDA(float* results);
FILE* openFile(const char *parent, const char *child,
const char* mode, long *length)
{
char name[strlen(parent) + strlen(child) + 1];
strcpy(name, parent);
strcat(name, child);
FILE *file = NULL;
if (!(file = fopen(name, mode)))
{
fprintf(stderr, "Failed to open '%s'\n", name);
exit(1);
}
if(length){
fseek(file, 0, SEEK_END);
*length = ftell(file);
rewind(file);
}
return file;
}
int main(int argc, char *argv[])
{
loadParameters(argc, argv);
printf("\n");
printf("Poses : %d\n", params.nposes);
printf("Iterations: %d\n", params.iterations);
printf("Ligands : %d\n", params.natlig);
printf("Proteins : %d\n", params.natpro);
printf("Deck : %s\n", params.deckDir);
float *resultsCUDA = malloc(params.nposes*sizeof(float));
float *resultsRef = malloc(params.nposes*sizeof(float));
runCUDA(resultsCUDA);
// Load reference results from file
FILE* ref_energies = openFile(params.deckDir, FILE_REF_ENERGIES, "r", NULL);
size_t n_ref_poses = params.nposes;
if (params.nposes > REF_NPOSES) {
printf("Only validating the first %d poses.\n", REF_NPOSES);
n_ref_poses = REF_NPOSES;
}
for (size_t i = 0; i < n_ref_poses; i++)
fscanf(ref_energies, "%f", &resultsRef[i]);
fclose(ref_energies);
float maxdiff = -100.0f;
printf("\n Reference CUDA (diff)\n");
for (int i = 0; i < n_ref_poses; i++)
{
if (fabs(resultsRef[i]) < 1.f && fabs(resultsCUDA[i]) < 1.f) continue;
float diff = fabs(resultsRef[i] - resultsCUDA[i]) / resultsCUDA[i];
if (diff > maxdiff) {
maxdiff = diff;
// printf ("Maxdiff: %.2f (%.3f vs %.3f)\n", maxdiff, resultsRef[i], resultsCUDA[i]);
}
if (i < 8)
printf("%7.2f vs %7.2f (%5.2f%%)\n", resultsRef[i], resultsCUDA[i], 100*diff);
}
printf("\nLargest difference was %.3f%%\n\n", maxdiff*100);
free(resultsCUDA);
free(resultsRef);
freeParameters();
}
int parseInt(const char *str)
{
char *next;
int value = strtoul(str, &next, 10);
return strlen(next) ? -1 : value;
}
void loadParameters(int argc, char *argv[])
{
// Defaults
params.deckDir = DATA_DIR;
params.iterations = 8;
_cuda.wgsize = 64;
_cuda.posesPerWI = 4;
int nposes = 65536;
for (int i = 1; i < argc; i++)
{
if (!strcmp(argv[i], "--device") || !strcmp(argv[i], "-d"))
{
if (++i >= argc || (_cuda.deviceIndex = parseInt(argv[i])) < 0)
{
printf("Invalid device index\n");
exit(1);
}
}
else if (!strcmp(argv[i], "--iterations") || !strcmp(argv[i], "-i"))
{
if (++i >= argc || (params.iterations = parseInt(argv[i])) < 0)
{
printf("Invalid number of iterations\n");
exit(1);
}
}
else if (!strcmp(argv[i], "--numposes") || !strcmp(argv[i], "-n"))
{
if (++i >= argc || (nposes = parseInt(argv[i])) < 0)
{
printf("Invalid number of poses\n");
exit(1);
}
}
else if (!strcmp(argv[i], "--posesperwi") || !strcmp(argv[i], "-p"))
{
if (++i >= argc || (_cuda.posesPerWI = parseInt(argv[i])) < 0)
{
printf("Invalid poses-per-workitem value\n");
exit(1);
}
}
else if (!strcmp(argv[i], "--wgsize") || !strcmp(argv[i], "-w"))
{
if (++i >= argc || (_cuda.wgsize = parseInt(argv[i])) < 0)
{
printf("Invalid work-group size\n");
exit(1);
}
}
else if (!strcmp(argv[i], "--deck"))
{
if (++i >= argc)
{
printf("Invalid deck\n");
exit(1);
}
params.deckDir = argv[i];
}
else if (!strcmp(argv[i], "--help") || !strcmp(argv[i], "-h"))
{
printf("\n");
printf("Usage: ./bude [OPTIONS]\n\n");
printf("Options:\n");
printf(" -h --help Print this message\n");
printf(" --list List available devices\n");
printf(" --device INDEX Select device at INDEX\n");
printf(" -i --iterations I Repeat kernel I times\n");
printf(" -n --numposes N Compute results for N poses\n");
printf(" -p --poserperwi PPWI Compute PPWI poses per work-item\n");
printf(" -w --wgsize WGSIZE Run with work-group size WGSIZE\n");
printf(" --deck DECK Use the DECK directory as input deck\n");
printf("\n");
exit(0);
}
else
{
printf("Unrecognized argument '%s' (try '--help')\n", argv[i]);
exit(1);
}
}
FILE *file = NULL;
long length;
file = openFile(params.deckDir, FILE_LIGAND, "rb", &length);
params.natlig = length / sizeof(Atom);
params.ligand = malloc(params.natlig*sizeof(Atom));
fread(params.ligand, sizeof(Atom), params.natlig, file);
fclose(file);
file = openFile(params.deckDir, FILE_PROTEIN, "rb", &length);
params.natpro = length / sizeof(Atom);
params.protein = malloc(params.natpro*sizeof(Atom));
fread(params.protein, sizeof(Atom), params.natpro, file);
fclose(file);
file = openFile(params.deckDir, FILE_FORCEFIELD, "rb", &length);
params.ntypes = length / sizeof(FFParams);
params.forcefield = malloc(params.ntypes*sizeof(FFParams));
fread(params.forcefield, sizeof(FFParams), params.ntypes, file);
fclose(file);
file = openFile(params.deckDir, FILE_POSES, "rb", &length);
for (int i = 0; i < 6; i++)
params.poses[i] = malloc(nposes*sizeof(float));
long available = length / 6 / sizeof(float);
params.nposes = 0;
while (params.nposes < nposes)
{
long fetch = nposes - params.nposes;
if (fetch > available)
fetch = available;
for (int i = 0; i < 6; i++)
{
fseek(file, i*available*sizeof(float), SEEK_SET);
fread(params.poses[i] + params.nposes, sizeof(float), fetch, file);
}
rewind(file);
params.nposes += fetch;
}
fclose(file);
}
void freeParameters()
{
free(params.ligand);
free(params.protein);
free(params.forcefield);
for (int i = 0; i < 6; i++)
free(params.poses[i]);
}