PSCF v1.5.0
cuda/CFieldComparison.tpp
1#ifndef PSCF_C_FIELD_COMPARISON_CU_TPP
2#define PSCF_C_FIELD_COMPARISON_CU_TPP
3
4/*
5* PSCF - Polymer Self-Consistent Field
6*
7* Copyright 2015 - 2026, David Morse (morse012@um.edu) and co-authors
8* Distributed under the terms of the GNU General Public License.
9*/
10
11#include "CFieldComparison.h"
12#include <pscf/backend/cuda/HostSendArray.h>
13#include <cmath>
14
15namespace Pscf {
16
17 // Default Constructor
18 template <int D>
20 : maxDiff_(0.0),
21 rmsDiff_(0.0)
22 {};
23
24 // Comparator for individual fields.
25 template <int D>
27 CField<D,CUT> const& b)
28 {
29 UTIL_CHECK(a.capacity() > 0);
30 UTIL_CHECK(a.capacity() == b.capacity());
31 int capacity = a.capacity();
32
33 // Allocate arrays on CPU host
36 ha.allocate(capacity);
37 hb.allocate(capacity);
38
39 // Copy field data from device to host
40 ha = a;
41 hb = b;
42
43 // Compare fields on host
44 double diffSq, diff, d0, d1;
45 maxDiff_ = 0.0;
46 rmsDiff_ = 0.0;
47 for (int i = 0; i < capacity; ++i) {
48 d0 = ha[i].x - hb[i].x;
49 d1 = ha[i].y - hb[i].y;
50 diffSq = d0*d0 + d1*d1;
51 diff = sqrt(diffSq);
52 if (diff > maxDiff_) {
53 maxDiff_ = diff;
54 }
55 rmsDiff_ += diffSq;
56 }
57 rmsDiff_ = rmsDiff_/double(capacity);
58 rmsDiff_ = sqrt(rmsDiff_);
59 return maxDiff_;
60 }
61
62 // Comparator for arrays of fields
63 template <int D>
65 DArray< CField<D,CUT> > const & b)
66 {
67 UTIL_CHECK(a.capacity() > 0);
68 UTIL_CHECK(a.capacity() == b.capacity());
69 UTIL_CHECK(a[0].capacity() > 0);
70 int capacity = a[0].capacity();
71 int nFields = a.capacity();
72
73 // Allocate arrays on host
76 ha.allocate(nFields);
77 hb.allocate(nFields);
78 for (int i = 0; i < nFields; i++) {
79 ha[i].allocate(capacity);
80 hb[i].allocate(capacity);
81 }
82
83 // Copy data from device to host
84 for (int i = 0; i < nFields; i++) {
85 ha[i] = a[i];
86 hb[i] = b[i];
87 }
88
89 // Perform comparison using host arrays
90 double diffSq, diff, d0, d1;
91 maxDiff_ = 0.0;
92 rmsDiff_ = 0.0;
93 int i, j;
94 for (i = 0; i < nFields; ++i) {
95 for (j = 0; j < capacity; ++j) {
96 d0 = ha[i][j].x - hb[i][j].x;
97 d1 = ha[i][j].y - hb[i][j].y;
98 diffSq = d0*d0 + d1*d1;
99 diff = sqrt(diffSq);
100 if (diff > maxDiff_) {
101 maxDiff_ = diff;
102 }
103 rmsDiff_ += diffSq;
104 }
105 }
106 rmsDiff_ = rmsDiff_/double(nFields*capacity);
107 rmsDiff_ = sqrt(rmsDiff_);
108 return maxDiff_;
109 }
110
111} // namespace Pscf
112#endif
Dynamically allocatable contiguous array template.
Definition DArray.h:32
void allocate(int capacity)
Allocate the underlying C array.
Definition DArray.h:262
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
Definition global.h:68
PSCF package top-level namespace.