-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathVectormath.cu
781 lines (706 loc) · 26.4 KB
/
Vectormath.cu
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
#ifdef SPIRIT_USE_CUDA
#include <engine/Backend_par.hpp>
#include <engine/Vectormath.hpp>
#include <utility/Constants.hpp>
#include <utility/Logging.hpp>
#include <utility/Exception.hpp>
#include <Eigen/Dense>
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <curand.h>
#include <curand_kernel.h>
#include <cub/cub.cuh>
using namespace Utility;
using Utility::Constants::Pi;
// CUDA Version
namespace Engine
{
namespace Vectormath
{
void get_random_vector(std::uniform_real_distribution<scalar> & distribution, std::mt19937 & prng, Vector3 & vec)
{
for (int dim = 0; dim < 3; ++dim)
{
vec[dim] = distribution(prng);
}
}
// TODO: improve random number generation - this one might give undefined behaviour!
__global__
void cu_get_random_vectorfield(Vector3 * xi, size_t N)
{
unsigned long long subsequence = 0;
unsigned long long offset= 0;
curandState_t state;
for(int idx = blockIdx.x * blockDim.x + threadIdx.x;
idx < N;
idx += blockDim.x * gridDim.x)
{
curand_init(idx,subsequence,offset,&state);
for (int dim=0;dim<3; ++dim)
{
xi[idx][dim] = llroundf(curand_uniform(&state))*2-1;
}
}
}
void get_random_vectorfield(std::uniform_real_distribution<scalar> & distribution, std::mt19937 & prng, vectorfield & xi)
{
int n = xi.size();
cu_get_random_vectorfield<<<(n+1023)/1024, 1024>>>(xi.data(), n);
CU_CHECK_AND_SYNC();
}
void get_random_vector_unitsphere(std::uniform_real_distribution<scalar> & distribution, std::mt19937 & prng, Vector3 & vec)
{
scalar v_z = distribution(prng);
scalar phi = distribution(prng);
scalar r_xy = std::sqrt(1 - v_z*v_z);
vec[0] = r_xy * std::cos(2*Pi*phi);
vec[1] = r_xy * std::sin(2*Pi*phi);
vec[2] = v_z;
}
// __global__ void cu_get_random_vectorfield_unitsphere(Vector3 * xi, size_t N)
// {
// unsigned long long subsequence = 0;
// unsigned long long offset= 0;
// curandState_t state;
// for(int idx = blockIdx.x * blockDim.x + threadIdx.x;
// idx < N;
// idx += blockDim.x * gridDim.x)
// {
// curand_init(idx,subsequence,offset,&state);
// scalar v_z = llroundf(curand_uniform(&state))*2-1;
// scalar phi = llroundf(curand_uniform(&state))*2-1;
// scalar r_xy = std::sqrt(1 - v_z*v_z);
// xi[idx][0] = r_xy * std::cos(2*Pi*phi);
// xi[idx][1] = r_xy * std::sin(2*Pi*phi);
// xi[idx][2] = v_z;
// }
// }
// void get_random_vectorfield_unitsphere(std::mt19937 & prng, vectorfield & xi)
// {
// int n = xi.size();
// cu_get_random_vectorfield<<<(n+1023)/1024, 1024>>>(xi.data(), n);
// CU_CHECK_AND_SYNC();
// }
// The above CUDA implementation does not work correctly.
void get_random_vectorfield_unitsphere(std::mt19937 & prng, vectorfield & xi)
{
// PRNG gives RN [-1,1] -> multiply with epsilon
auto distribution = std::uniform_real_distribution<scalar>(-1, 1);
// TODO: parallelization of this is actually not quite so trivial
#pragma omp parallel for
for (unsigned int i = 0; i < xi.size(); ++i)
{
get_random_vector_unitsphere(distribution, prng, xi[i]);
}
}
/////////////////////////////////////////////////////////////////
__global__ void cu_fill(scalar *sf, scalar s, size_t N)
{
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if(idx < N)
{
sf[idx] = s;
}
}
void fill(scalarfield & sf, scalar s)
{
int n = sf.size();
cu_fill<<<(n+1023)/1024, 1024>>>(sf.data(), s, n);
CU_CHECK_AND_SYNC();
}
__global__ void cu_fill_mask(scalar *sf, scalar s, const int * mask, size_t N)
{
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if(idx < N)
{
sf[idx] = mask[idx]*s;
}
}
void fill(scalarfield & sf, scalar s, const intfield & mask)
{
int n = sf.size();
cu_fill_mask<<<(n+1023)/1024, 1024>>>(sf.data(), s, mask.data(), n);
CU_CHECK_AND_SYNC();
}
__global__ void cu_scale(scalar *sf, scalar s, size_t N)
{
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if(idx < N)
{
sf[idx] *= s;
}
}
void scale(scalarfield & sf, scalar s)
{
int n = sf.size();
cu_scale<<<(n+1023)/1024, 1024>>>(sf.data(), s, n);
CU_CHECK_AND_SYNC();
}
__global__ void cu_add(scalar *sf, scalar s, size_t N)
{
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if(idx < N)
{
sf[idx] += s;
}
}
void add(scalarfield & sf, scalar s)
{
int n = sf.size();
cu_add<<<(n+1023)/1024, 1024>>>(sf.data(), s, n);
cudaDeviceSynchronize();
}
scalar sum(const scalarfield & sf)
{
static scalarfield ret(1, 0);
Vectormath::fill(ret, 0);
// Determine temporary storage size and allocate
void * d_temp_storage = NULL;
size_t temp_storage_bytes = 0;
cub::DeviceReduce::Sum(d_temp_storage, temp_storage_bytes, sf.data(), ret.data(), sf.size());
cudaMalloc(&d_temp_storage, temp_storage_bytes);
// Reduction
cub::DeviceReduce::Sum(d_temp_storage, temp_storage_bytes, sf.data(), ret.data(), sf.size());
CU_CHECK_AND_SYNC();
cudaFree(d_temp_storage);
return ret[0];
}
scalar mean(const scalarfield & sf)
{
return sum(sf)/sf.size();
}
__global__ void cu_divide(const scalar * numerator, const scalar * denominator, scalar * out, size_t N)
{
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if(idx < N)
{
out[idx] += numerator[idx] / denominator[idx];
}
}
void divide( const scalarfield & numerator, const scalarfield & denominator, scalarfield & out )
{
int n = numerator.size();
cu_divide<<<(n+1023)/1024, 1024>>>(numerator.data(), denominator.data(), out.data(), n);
CU_CHECK_AND_SYNC();
}
void set_range(scalarfield & sf, scalar sf_min, scalar sf_max)
{
#pragma omp parallel for
for (unsigned int i = 0; i<sf.size(); ++i)
sf[i] = std::min( std::max( sf_min, sf[i] ), sf_max );
}
__global__ void cu_fill(Vector3 *vf1, Vector3 v2, size_t N)
{
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if(idx < N)
{
vf1[idx] = v2;
}
}
void fill(vectorfield & vf, const Vector3 & v)
{
int n = vf.size();
cu_fill<<<(n+1023)/1024, 1024>>>(vf.data(), v, n);
CU_CHECK_AND_SYNC();
}
__global__ void cu_fill_mask(Vector3 *vf1, Vector3 v2, const int * mask, size_t N)
{
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if(idx < N)
{
vf1[idx] = v2;
}
}
void fill(vectorfield & vf, const Vector3 & v, const intfield & mask)
{
int n = vf.size();
cu_fill_mask<<<(n+1023)/1024, 1024>>>(vf.data(), v, mask.data(), n);
CU_CHECK_AND_SYNC();
}
__global__ void cu_normalize_vectors(Vector3 *vf, size_t N)
{
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if(idx < N)
{
vf[idx].normalize();
}
}
void normalize_vectors(vectorfield & vf)
{
int n = vf.size();
cu_normalize_vectors<<<(n+1023)/1024, 1024>>>(vf.data(), n);
CU_CHECK_AND_SYNC();
}
__global__ void cu_norm(const Vector3 * vf, scalar * norm, size_t N)
{
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if(idx < N)
{
norm[idx] = vf[idx].norm();
}
}
void norm( const vectorfield & vf, scalarfield & norm )
{
int n = vf.size();
cu_norm<<<(n+1023)/1024, 1024>>>(vf.data(), norm.data(), n);
CU_CHECK_AND_SYNC();
}
// Functor for finding the maximum absolute value
// struct CustomMaxAbs
// {
// template <typename T>
// __device__ __forceinline__
// T operator()(const T &a, const T &b) const {
// return (a > b) ? a : b;
// }
// };
scalar max_abs_component(const vectorfield & vf)
{
// Declare, allocate, and initialize device-accessible pointers for input and output
// CustomMaxAbs max_op;
size_t N = 3*vf.size();
scalarfield out(1, 0);
scalar init = 0;
// Determine temporary device storage requirements
void *d_temp_storage = NULL;
size_t temp_storage_bytes = 0;
auto lam = [] __device__ (const scalar & a, const scalar & b)
{
return (a > b) ? a : b;
};
cub::DeviceReduce::Reduce(d_temp_storage, temp_storage_bytes, vf[0].data(), out.data(), N, lam, init);
// Allocate temporary storage
cudaMalloc(&d_temp_storage, temp_storage_bytes);
// Run reduction
cub::DeviceReduce::Reduce(d_temp_storage, temp_storage_bytes, vf[0].data(), out.data(), N, lam, init);
CU_CHECK_AND_SYNC();
cudaFree(d_temp_storage);
return std::abs(out[0]);
}
scalar max_norm(const vectorfield & vf)
{
static scalarfield ret(1, 0);
// Declare, allocate, and initialize device-accessible pointers for input and output
size_t N = vf.size();
scalarfield temp(N, 0);
auto o = temp.data();
auto v = vf.data();
Backend::par::apply(N, [o,v] SPIRIT_LAMBDA (int idx) {
o[idx] = v[idx][0]*v[idx][0] + v[idx][1]*v[idx][1] + v[idx][2]*v[idx][2];
});
void *d_temp_storage = NULL;
size_t temp_storage_bytes = 0;
auto lam = [] __device__ (const scalar & a, const scalar & b)
{
return (a > b) ? a : b;
};
scalar init = 0;
cub::DeviceReduce::Reduce(d_temp_storage, temp_storage_bytes, temp.data(), ret.data(), N, lam, init);
// Allocate temporary storage
cudaMalloc(&d_temp_storage, temp_storage_bytes);
// Run reduction
cub::DeviceReduce::Reduce(d_temp_storage, temp_storage_bytes, temp.data(), ret.data(), N, lam, init);
CU_CHECK_AND_SYNC();
cudaFree(d_temp_storage);
return std::sqrt(ret[0]);
}
__global__ void cu_scale(Vector3 *vf1, scalar sc, size_t N)
{
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if(idx < N)
{
vf1[idx] *= sc;
}
}
void scale(vectorfield & vf, const scalar & sc)
{
int n = vf.size();
cu_scale<<<(n+1023)/1024, 1024>>>(vf.data(), sc, n);
CU_CHECK_AND_SYNC();
}
__global__ void cu_scale(Vector3 *vf1, const scalar * sf, bool inverse, size_t N)
{
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if(idx < N)
{
if( inverse )
vf1[idx] /= sf[idx];
else
vf1[idx] *= sf[idx];
}
}
void scale(vectorfield & vf, const scalarfield & sf, bool inverse)
{
int n = vf.size();
cu_scale<<<(n+1023)/1024, 1024>>>(vf.data(), sf.data(), inverse, n);
CU_CHECK_AND_SYNC();
}
// Functor for adding Vector3's
struct CustomAdd
{
template <typename T>
__device__ __forceinline__
T operator()(const T &a, const T &b) const {
return a + b;
}
};
Vector3 sum(const vectorfield & vf)
{
static vectorfield ret(1, {0,0,0});
Vectormath::fill(ret, {0,0,0});
// Declare, allocate, and initialize device-accessible pointers for input and output
CustomAdd add_op;
static const Vector3 init{0,0,0};
// Determine temporary device storage requirements
void *d_temp_storage = NULL;
size_t temp_storage_bytes = 0;
cub::DeviceReduce::Reduce(d_temp_storage, temp_storage_bytes, vf.data(), ret.data(), vf.size(), add_op, init);
// Allocate temporary storage
cudaMalloc(&d_temp_storage, temp_storage_bytes);
// Run reduction
cub::DeviceReduce::Reduce(d_temp_storage, temp_storage_bytes, vf.data(), ret.data(), vf.size(), add_op, init);
CU_CHECK_AND_SYNC();
cudaFree(d_temp_storage);
return ret[0];
}
Vector3 mean(const vectorfield & vf)
{
return sum(vf)/vf.size();
}
__global__ void cu_dot(const Vector3 *vf1, const Vector3 *vf2, scalar *out, size_t N)
{
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if(idx < N)
{
out[idx] = vf1[idx].dot(vf2[idx]);
}
}
scalar dot(const vectorfield & vf1, const vectorfield & vf2)
{
int n = vf1.size();
static scalarfield sf(n, 0);
if(sf.size() != vf1.size())
sf.resize(vf1.size());
Vectormath::fill(sf, 0);
scalar ret;
// Dot product
cu_dot<<<(n+1023)/1024, 1024>>>(vf1.data(), vf2.data(), sf.data(), n);
CU_CHECK_AND_SYNC();
// reduction
ret = sum(sf);
return ret;
}
void dot(const vectorfield & vf1, const vectorfield & vf2, scalarfield & s)
{
int n = vf1.size();
// Dot product
cu_dot<<<(n+1023)/1024, 1024>>>(vf1.data(), vf2.data(), s.data(), n);
CU_CHECK_AND_SYNC();
}
__global__ void cu_scalardot(const scalar * s1, const scalar * s2, scalar * out, size_t N)
{
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if(idx < N)
{
out[idx] = s1[idx] * s2[idx];
}
}
// computes the product of scalars in s1 and s2
// s1 and s2 are scalarfields
void dot( const scalarfield & s1, const scalarfield & s2, scalarfield & out )
{
int n = s1.size();
// Dot product
cu_scalardot<<<(n+1023)/1024, 1024>>>(s1.data(), s2.data(), out.data(), n);
CU_CHECK_AND_SYNC();
}
__global__ void cu_cross(const Vector3 *vf1, const Vector3 *vf2, Vector3 *out, size_t N)
{
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if(idx < N)
{
out[idx] = vf1[idx].cross(vf2[idx]);
}
}
// The wrapper for the calling of the actual kernel
void cross(const vectorfield & vf1, const vectorfield & vf2, vectorfield & s)
{
int n = vf1.size();
// Dot product
cu_cross<<<(n+1023)/1024, 1024>>>(vf1.data(), vf2.data(), s.data(), n);
CU_CHECK_AND_SYNC();
}
__global__ void cu_add_c_a(scalar c, Vector3 a, Vector3 * out, size_t N)
{
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if(idx < N)
{
out[idx] += c*a;
}
}
// out[i] += c*a
void add_c_a(const scalar & c, const Vector3 & a, vectorfield & out)
{
int n = out.size();
cu_add_c_a<<<(n+1023)/1024, 1024>>>(c, a, out.data(), n);
CU_CHECK_AND_SYNC();
}
__global__ void cu_add_c_a2(scalar c, const Vector3 * a, Vector3 * out, size_t N)
{
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if(idx < N)
{
out[idx] += c*a[idx];
}
}
// out[i] += c*a[i]
void add_c_a(const scalar & c, const vectorfield & a, vectorfield & out)
{
int n = out.size();
cu_add_c_a2<<<(n+1023)/1024, 1024>>>(c, a.data(), out.data(), n);
CU_CHECK_AND_SYNC();
}
__global__ void cu_add_c_a2_mask(scalar c, const Vector3 * a, Vector3 * out, const int * mask, size_t N)
{
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if(idx < N)
{
out[idx] += c*mask[idx]*a[idx];
}
}
// out[i] += c*a[i]
void add_c_a(const scalar & c, const vectorfield & a, vectorfield & out, const intfield & mask)
{
int n = out.size();
cu_add_c_a2_mask<<<(n+1023)/1024, 1024>>>(c, a.data(), out.data(), mask.data(), n);
CU_CHECK_AND_SYNC();
}
__global__ void cu_add_c_a3(const scalar * c, const Vector3 * a, Vector3 * out, size_t N)
{
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if(idx < N)
{
out[idx] += c[idx]*a[idx];
}
}
// out[i] += c[i]*a[i]
void add_c_a( const scalarfield & c, const vectorfield & a, vectorfield & out )
{
int n = out.size();
cu_add_c_a3<<<(n+1023)/1024, 1024>>>(c.data(), a.data(), out.data(), n);
CU_CHECK_AND_SYNC();
}
__global__ void cu_set_c_a(scalar c, Vector3 a, Vector3 * out, size_t N)
{
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if(idx < N)
{
out[idx] = c*a;
}
}
// out[i] = c*a
void set_c_a(const scalar & c, const Vector3 & a, vectorfield & out)
{
int n = out.size();
cu_set_c_a<<<(n+1023)/1024, 1024>>>(c, a, out.data(), n);
CU_CHECK_AND_SYNC();
}
__global__ void cu_set_c_a_mask(scalar c, Vector3 a, Vector3 * out, const int * mask, size_t N)
{
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if(idx < N)
{
out[idx] = mask[idx]*c*a;
}
}
// out[i] = c*a
void set_c_a(const scalar & c, const Vector3 & a, vectorfield & out, const intfield & mask)
{
int n = out.size();
cu_set_c_a_mask<<<(n+1023)/1024, 1024>>>(c, a, out.data(), mask.data(), n);
CU_CHECK_AND_SYNC();
}
__global__ void cu_set_c_a2(scalar c, const Vector3 * a, Vector3 * out, size_t N)
{
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if(idx < N)
{
out[idx] = c*a[idx];
}
}
// out[i] = c*a[i]
void set_c_a(const scalar & c, const vectorfield & a, vectorfield & out)
{
int n = out.size();
cu_set_c_a2<<<(n+1023)/1024, 1024>>>(c, a.data(), out.data(), n);
CU_CHECK_AND_SYNC();
}
__global__ void cu_set_c_a2_mask(scalar c, const Vector3 * a, Vector3 * out, const int * mask, size_t N)
{
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if(idx < N)
{
out[idx] = mask[idx]*c*a[idx];
}
}
// out[i] = c*a[i]
void set_c_a(const scalar & c, const vectorfield & a, vectorfield & out, const intfield & mask)
{
int n = out.size();
cu_set_c_a2_mask<<<(n+1023)/1024, 1024>>>(c, a.data(), out.data(), mask.data(), n);
CU_CHECK_AND_SYNC();
}
__global__ void cu_set_c_a3(const scalar * c, const Vector3 * a, Vector3 * out, size_t N)
{
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if(idx < N)
{
out[idx] = c[idx]*a[idx];
}
}
// out[i] = c[i]*a[i]
void set_c_a( const scalarfield & c, const vectorfield & a, vectorfield & out )
{
int n = out.size();
cu_set_c_a3<<<(n+1023)/1024, 1024>>>(c.data(), a.data(), out.data(), n);
CU_CHECK_AND_SYNC();
}
__global__ void cu_add_c_dot(scalar c, Vector3 a, const Vector3 * b, scalar * out, size_t N)
{
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if(idx < N)
{
out[idx] += c*a.dot(b[idx]);
}
}
// out[i] += c * a*b[i]
void add_c_dot(const scalar & c, const Vector3 & a, const vectorfield & b, scalarfield & out)
{
int n = out.size();
cu_add_c_dot<<<(n+1023)/1024, 1024>>>(c, a, b.data(), out.data(), n);
CU_CHECK_AND_SYNC();
}
__global__ void cu_add_c_dot(scalar c, const Vector3 * a, const Vector3 * b, scalar * out, size_t N)
{
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if(idx < N)
{
out[idx] += c*a[idx].dot(b[idx]);
}
}
// out[i] += c * a[i]*b[i]
void add_c_dot(const scalar & c, const vectorfield & a, const vectorfield & b, scalarfield & out)
{
int n = out.size();
cu_add_c_dot<<<(n+1023)/1024, 1024>>>(c, a.data(), b.data(), out.data(), n);
CU_CHECK_AND_SYNC();
}
__global__ void cu_set_c_dot(scalar c, Vector3 a, const Vector3 * b, scalar * out, size_t N)
{
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if(idx < N)
{
out[idx] = c*a.dot(b[idx]);
}
}
// out[i] = c * a*b[i]
void set_c_dot(const scalar & c, const Vector3 & a, const vectorfield & b, scalarfield & out)
{
int n = out.size();
cu_set_c_dot<<<(n+1023)/1024, 1024>>>(c, a, b.data(), out.data(), n);
CU_CHECK_AND_SYNC();
}
__global__ void cu_set_c_dot(scalar c, const Vector3 * a, const Vector3 * b, scalar * out, size_t N)
{
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if(idx < N)
{
out[idx] = c*a[idx].dot(b[idx]);
}
}
// out[i] = c * a[i]*b[i]
void set_c_dot(const scalar & c, const vectorfield & a, const vectorfield & b, scalarfield & out)
{
int n = out.size();
cu_set_c_dot<<<(n+1023)/1024, 1024>>>(c, a.data(), b.data(), out.data(), n);
CU_CHECK_AND_SYNC();
}
// out[i] += c * a x b[i]
__global__ void cu_add_c_cross(scalar c, const Vector3 a, const Vector3 * b, Vector3 * out, size_t N)
{
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if(idx < N)
{
out[idx] += c*a.cross(b[idx]);
}
}
void add_c_cross(const scalar & c, const Vector3 & a, const vectorfield & b, vectorfield & out)
{
int n = out.size();
cu_add_c_cross<<<(n+1023)/1024, 1024>>>(c, a, b.data(), out.data(), n);
CU_CHECK_AND_SYNC();
}
// out[i] += c * a[i] x b[i]
__global__ void cu_add_c_cross(scalar c, const Vector3 * a, const Vector3 * b, Vector3 * out, size_t N)
{
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if(idx < N)
{
out[idx] += c*a[idx].cross(b[idx]);
}
}
void add_c_cross(const scalar & c, const vectorfield & a, const vectorfield & b, vectorfield & out)
{
int n = out.size();
cu_add_c_cross<<<(n+1023)/1024, 1024>>>(c, a.data(), b.data(), out.data(), n);
CU_CHECK_AND_SYNC();
}
// out[i] += c * a[i] x b[i]
__global__ void cu_add_c_cross(const scalar * c, const Vector3 * a, const Vector3 * b, Vector3 * out, size_t N)
{
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if(idx < N)
{
out[idx] += c[idx]*a[idx].cross(b[idx]);
}
}
void add_c_cross(const scalarfield & c, const vectorfield & a, const vectorfield & b, vectorfield & out)
{
int n = out.size();
cu_add_c_cross<<<(n+1023)/1024, 1024>>>(c.data(), a.data(), b.data(), out.data(), n);
cudaDeviceSynchronize();
}
// out[i] = c * a x b[i]
__global__ void cu_set_c_cross(scalar c, const Vector3 a, const Vector3 * b, Vector3 * out, size_t N)
{
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if(idx < N)
{
out[idx] = c*a.cross(b[idx]);
}
}
void set_c_cross(const scalar & c, const Vector3 & a, const vectorfield & b, vectorfield & out)
{
int n = out.size();
cu_set_c_cross<<<(n+1023)/1024, 1024>>>(c, a, b.data(), out.data(), n);
CU_CHECK_AND_SYNC();
}
// out[i] = c * a[i] x b[i]
__global__ void cu_set_c_cross(scalar c, const Vector3 * a, const Vector3 * b, Vector3 * out, size_t N)
{
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if(idx < N)
{
out[idx] = c*a[idx].cross(b[idx]);
}
}
void set_c_cross(const scalar & c, const vectorfield & a, const vectorfield & b, vectorfield & out)
{
int n = out.size();
cu_set_c_cross<<<(n+1023)/1024, 1024>>>(c, a.data(), b.data(), out.data(), n);
CU_CHECK_AND_SYNC();
}
}
}
#endif