154 const bool orthorhombic,
const int border_mask,
const double ra[3],
155 const double rab[3],
const double radius,
const grid_basis_set *basis_set_a,
157 const int la_max,
const int lb_max,
const int cycles,
158 const int cycles_per_block,
const int npts_global[][3],
159 const int npts_local[][3],
const int shift_local[][3],
160 const int border_width[][3],
const double dh[][3][3],
163 const int ntasks = cycles;
164 const int nlevels = 1;
165 const int natoms = 2;
166 const int nkinds = 2;
167 int nblocks = cycles / cycles_per_block + 1;
174 int block_offsets[nblocks];
175 memset(block_offsets, 0, nblocks *
sizeof(
int));
176 const double atom_positions[2][3] = {
177 {ra[0], ra[1], ra[2]}, {rab[0] + ra[0], rab[1] + ra[1], rab[2] + ra[2]}};
178 const int atom_kinds[2] = {1, 2};
180 const int ipgf = o1 / ncoset(la_max) + 1;
181 const int jpgf = o2 / ncoset(lb_max) + 1;
182 assert(o1 == (ipgf - 1) * ncoset(la_max));
183 assert(o2 == (jpgf - 1) * ncoset(lb_max));
185 int level_list[ntasks], iatom_list[ntasks], jatom_list[ntasks];
186 int iset_list[ntasks], jset_list[ntasks], ipgf_list[ntasks],
188 int border_mask_list[ntasks], block_num_list[ntasks];
189 double radius_list[ntasks], rab_list_mutable[ntasks][3];
190 for (
int i = 0;
i < cycles;
i++) {
198 border_mask_list[
i] = border_mask;
199 block_num_list[
i] =
i / cycles_per_block + 1;
200 radius_list[
i] = radius;
201 rab_list_mutable[
i][0] = rab[0];
202 rab_list_mutable[
i][1] = rab[1];
203 rab_list_mutable[
i][2] = rab[2];
205 const double(*rab_list)[3] = (
const double(*)[3])rab_list_mutable;
207 grid_create_task_list(
208 orthorhombic, ntasks, nlevels, natoms, nkinds, nblocks, block_offsets,
209 atom_positions, atom_kinds, basis_sets, level_list, iatom_list,
210 jatom_list, iset_list, jset_list, ipgf_list, jpgf_list, border_mask_list,
211 block_num_list, radius_list, rab_list, npts_global,
npts_local,
212 shift_local, border_width, dh, dh_inv, task_list);
220bool grid_replay(
const char *filename,
const int cycles,
const bool collocate,
221 const bool batch,
const int cycles_per_block,
222 const double tolerance) {
225 fprintf(stderr,
"Error: Cycles have to be greater than zero.\n");
229 if (cycles_per_block < 1 || cycles_per_block > cycles) {
231 "Error: Cycles per block has to be between 1 and cycles.\n");
235 FILE *fp = fopen(filename,
"r");
237 fprintf(stderr,
"Could not open task file: %s\n", filename);
241 char header_line[100];
243 if (strcmp(header_line,
"#Grid task v10\n") != 0) {
244 fprintf(stderr,
"Error: Wrong file header.\n");
248 const bool orthorhombic =
parse_int(
"orthorhombic", fp);
249 const int border_mask =
parse_int(
"border_mask", fp);
252 const int la_max =
parse_int(
"la_max", fp);
253 const int la_min =
parse_int(
"la_min", fp);
254 const int lb_max =
parse_int(
"lb_max", fp);
255 const int lb_min =
parse_int(
"lb_min", fp);
260 double dh_mutable[3][3], dh_inv_mutable[3][3], ra[3], rab[3];
265 const double(*dh)[3] = (
const double(*)[3])dh_mutable;
266 const double(*dh_inv)[3] = (
const double(*)[3])dh_inv_mutable;
268 int npts_global[3],
npts_local[3], shift_local[3], border_width[3];
280 double pab_mutable[n2][n1];
281 char line[100], format[100];
282 for (
int i = 0;
i < n2;
i++) {
283 for (
int j = 0; j < n1; j++) {
285 snprintf(format,
sizeof(format),
"pab %i %i %%lf",
i, j);
286 if (sscanf(line, format, &pab_mutable[
i][j]) != 1) {
287 assert(!
"parse_pab failed");
291 const double(*pab)[n1] = (
const double(*)[n1])pab_mutable;
295 offload_create_buffer(npts_local_total, &grid_ref);
296 memset(grid_ref->
host_buffer, 0, npts_local_total *
sizeof(
double));
298 const int ngrid_nonzero =
parse_int(
"ngrid_nonzero", fp);
299 for (
int n = 0; n < ngrid_nonzero; n++) {
303 if (sscanf(line,
"grid %i %i %i %le", &
i, &j, &k, &value) != 4) {
304 assert(!
"parse_grid failed");
310 double hab_ref[n2][n1];
311 memset(hab_ref, 0, n2 * n1 *
sizeof(
double));
312 for (
int i = o2;
i < ncoset(lb_max) + o2;
i++) {
313 for (
int j = o1; j < ncoset(la_max) + o1; j++) {
315 snprintf(format,
sizeof(format),
"hab %i %i %%lf",
i, j);
316 if (sscanf(line, format, &hab_ref[
i][j]) != 1) {
317 assert(!
"parse_hab failed");
322 double forces_ref[2][3];
326 double virial_ref[3][3];
329 char footer_line[100];
331 if (strcmp(footer_line,
"#THE_END\n") != 0) {
332 fprintf(stderr,
"Error: Wrong footer line.\n");
336 if (fclose(fp) != 0) {
337 fprintf(stderr,
"Could not close task file: %s\n", filename);
342 offload_create_buffer(npts_local_total, &grid_test);
343 double hab_test[n2][n1];
344 double forces_test[2][3];
345 double virial_test[3][3];
346 double start_time, end_time;
354 orthorhombic, border_mask, ra, rab, radius, basisa, basisb, o1, o2,
355 la_max, lb_max, cycles, cycles_per_block, (
const int(*)[3])npts_global,
356 (
const int(*)[3])
npts_local, (
const int(*)[3])shift_local,
357 (
const int(*)[3])border_width, (
const double(*)[3][3])dh,
358 (
const double(*)[3][3])dh_inv, &task_list);
360 offload_create_buffer(n1 * n2, &pab_blocks);
361 offload_create_buffer(n1 * n2, &hab_blocks);
362 const double f = (collocate) ? rscale : 1.0;
363 for (
int i = 0;
i < n1;
i++) {
364 for (
int j = 0; j < n2; j++) {
368 start_time = omp_get_wtime();
369 const int nlevels = 1;
370 const int natoms = 2;
374 grid_collocate_task_list(task_list, func, nlevels,
375 (
const int(*)[3])
npts_local, pab_blocks, grids);
379 grid_integrate_task_list(task_list, compute_tau, natoms, nlevels,
380 (
const int(*)[3])
npts_local, pab_blocks, grids,
381 hab_blocks, forces_test, virial_test);
382 for (
int i = 0;
i < n2;
i++) {
383 for (
int j = 0; j < n1; j++) {
384 hab_test[
i][j] = hab_blocks->host_buffer[
i * n1 + j];
388 end_time = omp_get_wtime();
391 grid_free_task_list(task_list);
392 offload_free_buffer(pab_blocks);
393 offload_free_buffer(hab_blocks);
395 start_time = omp_get_wtime();
398 memset(grid_test->
host_buffer, 0, npts_local_total *
sizeof(
double));
399 for (
int i = 0;
i < cycles;
i++) {
401 orthorhombic, border_mask, func, la_max, la_min, lb_max, lb_min,
402 zeta, zetb, rscale, dh, dh_inv, ra, rab, npts_global,
npts_local,
403 shift_local, border_width, radius, o1, o2, n1, n2, pab,
408 memset(hab_test, 0, n2 * n1 *
sizeof(
double));
409 memset(forces_test, 0, 2 * 3 *
sizeof(
double));
410 double virials_test[2][3][3] = {0};
411 for (
int i = 0;
i < cycles;
i++) {
413 orthorhombic, compute_tau, border_mask, la_max, la_min, lb_max,
414 lb_min, zeta, zetb, dh, dh_inv, ra, rab, npts_global,
npts_local,
415 shift_local, border_width, radius, o1, o2, n1, n2,
416 grid_ref->
host_buffer, hab_test, pab, forces_test, virials_test,
419 for (
int i = 0;
i < 3;
i++) {
420 for (
int j = 0; j < 3; j++) {
421 virial_test[
i][j] = virials_test[0][
i][j] + virials_test[1][
i][j];
425 end_time = omp_get_wtime();
428 double max_value = 0.0;
429 double max_rel_diff = 0.0;
430 const double derivatives_precision = 1e-4;
434 for (
int i = 0;
i < npts_local_total;
i++) {
435 const double ref_value = cycles * grid_ref->
host_buffer[
i];
437 const double diff = fabs(test_value - ref_value);
438 const double rel_diff = diff / fmax(1.0, fabs(ref_value));
439 max_rel_diff = fmax(max_rel_diff, rel_diff);
440 max_value = fmax(max_value, fabs(test_value));
445 for (
int i = 0;
i < n2;
i++) {
446 for (
int j = 0; j < n1; j++) {
447 const double ref_value = cycles * hab_ref[
i][j];
448 const double test_value = hab_test[
i][j];
449 const double diff = fabs(test_value - ref_value);
450 const double rel_diff = diff / fmax(1.0, fabs(ref_value));
451 max_rel_diff = fmax(max_rel_diff, rel_diff);
452 max_value = fmax(max_value, fabs(test_value));
453 if (rel_diff > tolerance) {
454 printf(
"hab[%i, %i] ref: %le test: %le diff:%le rel_diff: %le\n",
i,
455 j, ref_value, test_value, diff, rel_diff);
460 for (
int i = 0;
i < 2;
i++) {
461 for (
int j = 0; j < 3; j++) {
462 const double ref_value = cycles * forces_ref[
i][j];
463 const double test_value = forces_test[
i][j];
464 const double diff = fabs(test_value - ref_value);
465 const double rel_diff = diff / fmax(1.0, fabs(ref_value));
466 max_rel_diff = fmax(max_rel_diff, rel_diff * derivatives_precision);
467 if (rel_diff * derivatives_precision > tolerance) {
468 printf(
"forces[%i, %i] ref: %le test: %le diff:%le rel_diff: %le\n",
469 i, j, ref_value, test_value, diff, rel_diff);
474 for (
int i = 0;
i < 3;
i++) {
475 for (
int j = 0; j < 3; j++) {
476 const double ref_value = cycles * virial_ref[
i][j];
477 const double test_value = virial_test[
i][j];
478 const double diff = fabs(test_value - ref_value);
479 const double rel_diff = diff / fmax(1.0, fabs(ref_value));
480 max_rel_diff = fmax(max_rel_diff, rel_diff * derivatives_precision);
481 if (rel_diff * derivatives_precision > tolerance) {
482 printf(
"virial[ %i, %i] ref: %le test: %le diff:%le rel_diff: %le\n",
483 i, j, ref_value, test_value, diff, rel_diff);
488 printf(
"Task: %-55s %9s %-7s Cycles: %e Max value: %le "
489 "Max rel diff: %le Time: %le sec\n",
490 filename, collocate ?
"Collocate" :
"Integrate",
491 batch ?
"Batched" :
"PGF-CPU", (float)cycles, max_value, max_rel_diff,
492 end_time - start_time);
494 offload_free_buffer(grid_ref);
495 offload_free_buffer(grid_test);
498 if (fetestexcept(FE_DIVBYZERO) != 0) {
499 fprintf(stderr,
"Error: Floating point exception FE_DIVBYZERO.\n");
502 if (fetestexcept(FE_OVERFLOW) != 0) {
503 fprintf(stderr,
"Error: Floating point exception FE_OVERFLOW.\n");
507 return max_rel_diff < tolerance;
void grid_cpu_collocate_pgf_product(const bool orthorhombic, const int border_mask, const enum grid_func func, const int la_max, const int la_min, const int lb_max, const int lb_min, const double zeta, const double zetb, const double rscale, const double dh[3][3], const double dh_inv[3][3], const double ra[3], const double rab[3], const int npts_global[3], const int npts_local[3], const int shift_local[3], const int border_width[3], const double radius, const int o1, const int o2, const int n1, const int n2, const double pab[n2][n1], double *grid)
Public entry point. A thin wrapper with the only purpose of calling write_task_file when DUMP_TASKS =...
void grid_cpu_integrate_pgf_product(const bool orthorhombic, const bool compute_tau, const int border_mask, const int la_max, const int la_min, const int lb_max, const int lb_min, const double zeta, const double zetb, const double dh[3][3], const double dh_inv[3][3], const double ra[3], const double rab[3], const int npts_global[3], const int npts_local[3], const int shift_local[3], const int border_width[3], const double radius, const int o1, const int o2, const int n1, const int n2, const double *grid, double hab[n2][n1], const double pab[n2][n1], double forces[2][3], double virials[2][3][3], double hdab[n2][n1][3], double hadb[n2][n1][3], double a_hdab[n2][n1][3][3])
Integrates a single task. See grid_cpu_integrate.h for details.