/
KokkosSparse_pcg.hpp
569 lines (466 loc) · 17.6 KB
/
KokkosSparse_pcg.hpp
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
/*
//@HEADER
// ************************************************************************
//
// Kokkos v. 3.0
// Copyright (2020) National Technology & Engineering
// Solutions of Sandia, LLC (NTESS).
//
// Under the terms of Contract DE-NA0003525 with NTESS,
// the U.S. Government retains certain rights in this software.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the Corporation nor the names of the
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY NTESS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NTESS OR THE
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Questions? Contact Siva Rajamanickam (srajama@sandia.gov)
//
// ************************************************************************
//@HEADER
*/
#ifndef KOKKOS_EXAMPLE_CG_SOLVE
#define KOKKOS_EXAMPLE_CG_SOLVE
#include <cmath>
#include <limits>
#include <Kokkos_Core.hpp>
#include <Kokkos_Timer.hpp>
#include <Kokkos_Atomic.hpp>
#include <Kokkos_MemoryTraits.hpp>
#include <iostream>
#include "KokkosKernels_Handle.hpp"
#include <KokkosSparse_spmv.hpp>
#include <KokkosBlas.hpp>
#include <KokkosSparse_gauss_seidel.hpp>
#include <KokkosSparse_sor_sequential_impl.hpp>
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
//#define KK_TICTOCPRINT
namespace KokkosKernels {
namespace Experimental{
namespace Example {
struct CGSolveResult {
size_t iteration ;
double iter_time ;
double matvec_time ;
double norm_res ;
double precond_time;
double precond_init_time;
};
template< typename KernelHandle_t,
typename crsMatrix_t,
typename y_vector_t,
typename x_vector_t
>
void block_pcgsolve(
KernelHandle_t &kh
, const crsMatrix_t &point_crsMat
, const crsMatrix_t &_block_crsMat, int block_size
, const y_vector_t &y_vector
, x_vector_t x_vector
, const size_t maximum_iteration = 200
, const double tolerance = std::numeric_limits<double>::epsilon()
, CGSolveResult * result = 0
, bool use_sgs = true)
{
using namespace KokkosSparse;
using namespace KokkosSparse::Experimental;
typedef typename KernelHandle_t::HandleExecSpace Space;
const size_t count_total = point_crsMat.numRows();
size_t iteration = 0 ;
double iter_time = 0 ;
double matvec_time = 0 ;
double norm_res = 0 ;
double precond_time = 0;
double precond_init_time = 0;
Kokkos::Impl::Timer wall_clock ;
Kokkos::Impl::Timer timer;
// Need input vector to matvec to be owned + received
y_vector_t pAll ( "cg::p" , count_total );
y_vector_t p = Kokkos::subview( pAll , std::pair<size_t,size_t>(0,count_total) );
y_vector_t r ( "cg::r" , count_total );
y_vector_t Ap( "cg::Ap", count_total );
// r = b - A * x ;
// p = x
Kokkos::deep_copy( p , x_vector );
// Ap = A * p
KokkosSparse::spmv("N", 1, point_crsMat, pAll, 0, Ap);
// r = Ap
Kokkos::deep_copy( r , Ap );
// r = b - r
KokkosBlas::axpby(1.0, y_vector, -1.0, r);
// p = r
Kokkos::deep_copy( p , r );
;
double old_rdot = KokkosBlas::dot( r , r );
norm_res = sqrt( old_rdot );
int apply_count = 1;
y_vector_t z;
double precond_old_rdot = 1;
//Kokkos::deep_copy( p , z );
bool owner_handle = false;
KernelHandle_t block_kh;
block_kh.create_gs_handle();
block_kh.get_point_gs_handle()->set_block_size(block_size);
//block_kh.set_shmem_size(8032);
if (use_sgs){
if (kh.get_gs_handle() == NULL){
owner_handle = true;
kh.create_gs_handle();
}
timer.reset();
//gauss_seidel_numeric
// (&kh, count_total, count_total, point_crsMat.graph.row_map, point_crsMat.graph.entries, point_crsMat.values);
//Space().fence();
//timer.reset();
//block_kh.set_verbose(true);
block_gauss_seidel_numeric
(&block_kh, _block_crsMat.numRows(), _block_crsMat.numCols(), block_size, _block_crsMat.graph.row_map, _block_crsMat.graph.entries, _block_crsMat.values);
precond_init_time += timer.seconds();
z = y_vector_t( "pcg::z" , count_total );
Space().fence();
timer.reset();
symmetric_block_gauss_seidel_apply
(&block_kh, _block_crsMat.numRows(), _block_crsMat.numCols(),block_size, _block_crsMat.graph.row_map, _block_crsMat.graph.entries, _block_crsMat.values,
z, r, true, true, 1.0, apply_count);
//symmetric_gauss_seidel_apply
// (&kh, count_total, count_total, point_crsMat.graph.row_map, point_crsMat.graph.entries, point_crsMat.values, z, r, true, true, apply_count);
Space().fence();
precond_time += timer.seconds();
precond_old_rdot = KokkosBlas::dot( r , z );
Kokkos::deep_copy( p , z );
}
iteration = 0 ;
#ifdef KK_TICTOCPRINT
std::cout << "norm_res:" << norm_res << " old_rdot:" << old_rdot<< std::endl;
#endif
while ( tolerance < norm_res && iteration < maximum_iteration ) {
timer.reset();
//Ap = A * p
KokkosSparse::spmv("N", 1, point_crsMat, pAll, 0, Ap);
Space().fence();
matvec_time += timer.seconds();
//const double pAp_dot = Kokkos::Example::all_reduce( dot( count_owned , p , Ap ) , import.comm );
//const double pAp_dot = dot<y_vector_t,y_vector_t, Space>( count_total , p , Ap ) ;
// pAp_dot = dot(Ap , p);
const double pAp_dot = KokkosBlas::dot( p , Ap ) ;
double alpha = 0;
if (use_sgs){
alpha = precond_old_rdot / pAp_dot ;
}
else {
alpha = old_rdot / pAp_dot ;
}
// x += alpha * p ;
KokkosBlas::axpby(alpha, p, 1.0, x_vector);
// r += -alpha * Ap ;
KokkosBlas::axpby(-alpha, Ap, 1.0, r);
const double r_dot = KokkosBlas::dot( r , r );
const double beta_original = r_dot / old_rdot ;
double precond_r_dot = 1;
double precond_beta = 1;
if (use_sgs){
Space().fence();
timer.reset();
symmetric_block_gauss_seidel_apply
(&block_kh, _block_crsMat.numRows(), _block_crsMat.numCols(),block_size, _block_crsMat.graph.row_map, _block_crsMat.graph.entries, _block_crsMat.values,
z, r, true, true, 1.0, apply_count);
//symmetric_gauss_seidel_apply(
// &kh,
// count_total, count_total,
// point_crsMat.graph.row_map,
// point_crsMat.graph.entries,
// point_crsMat.values, z, r, true,
// apply_count);
Space().fence();
precond_time += timer.seconds();
precond_r_dot = KokkosBlas::dot(r , z );
precond_beta = precond_r_dot / precond_old_rdot ;
}
double beta = 1;
if (!use_sgs){
beta = beta_original;
// p = r + beta * p ;
KokkosBlas::axpby(1.0, r, beta, p);
}
else {
beta = precond_beta;
KokkosBlas::axpby(1.0, z, beta, p);
}
#ifdef KK_TICTOCPRINT
std::cout << "\tbeta_original:" << beta_original << std::endl;
if (use_sgs)
std::cout << "\tprecond_beta:" << precond_beta << std::endl;
#endif
norm_res = sqrt( old_rdot = r_dot );
precond_old_rdot = precond_r_dot;
#ifdef KK_TICTOCPRINT
std::cout << "\tnorm_res:" << norm_res << " old_rdot:" << old_rdot<< std::endl;
#endif
++iteration ;
}
Space().fence();
iter_time = wall_clock.seconds();
if ( 0 != result ) {
result->iteration = iteration ;
result->iter_time = iter_time ;
result->matvec_time = matvec_time ;
result->norm_res = norm_res ;
result->precond_time = precond_time;
result->precond_init_time = precond_init_time;
}
if (use_sgs & owner_handle ){
kh.destroy_gs_handle();
}
}
template< typename KernelHandle_t,
typename crsMatrix_t,
typename y_vector_t,
typename x_vector_t
>
void pcgsolve(
KernelHandle_t &kh
, const crsMatrix_t &crsMat
, const y_vector_t &y_vector
, x_vector_t x_vector
, const size_t maximum_iteration = 200
, const double tolerance = std::numeric_limits<double>::epsilon()
, CGSolveResult * result = 0
, bool use_sgs = true
, int /*clusterSize*/ = 1
, bool use_sequential_sgs = false)
{
using namespace KokkosSparse;
using namespace KokkosSparse::Experimental;
using size_type = typename KernelHandle_t::size_type;
using nnz_lno_t = typename KernelHandle_t::nnz_lno_t;
using Space = typename KernelHandle_t::HandleExecSpace;
static_assert(std::is_same<double, typename KernelHandle_t::nnz_scalar_t>::value,
"The PCG performance test only works with scalar = double.");
const nnz_lno_t count_total = crsMat.numRows();
size_t iteration = 0 ;
double iter_time = 0 ;
double matvec_time = 0 ;
double norm_res = 0 ;
double precond_time = 0;
double precond_init_time = 0;
Kokkos::Impl::Timer wall_clock ;
Kokkos::Impl::Timer timer;
// Need input vector to matvec to be owned + received
y_vector_t pAll ( "cg::p" , count_total );
y_vector_t p = Kokkos::subview( pAll , std::pair<size_t,size_t>(0,count_total) );
y_vector_t r ( "cg::r" , count_total );
y_vector_t Ap( "cg::Ap", count_total );
/* r = b - A * x ; */
/* p = x */ Kokkos::deep_copy( p , x_vector );
/* Ap = A * p */ KokkosSparse::spmv("N", 1, crsMat, pAll, 0, Ap);
/* r = Ap */ Kokkos::deep_copy( r , Ap );
/* r = b - r */ KokkosBlas::axpby(1.0, y_vector, -1.0, r);
/* p = r */ Kokkos::deep_copy( p , r );
double old_rdot = KokkosBlas::dot( r , r );
norm_res = sqrt( old_rdot );
int apply_count = 1;
y_vector_t z;
double precond_old_rdot = 1;
//Kokkos::deep_copy( p , z );
bool use_par_sgs = use_sgs && !use_sequential_sgs;
auto ptrHost = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), crsMat.graph.row_map);
auto indHost = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), crsMat.graph.entries);
auto valHost = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), crsMat.values);
Kokkos::View<double*, Kokkos::HostSpace> diagHost;
if(use_sequential_sgs)
{
diagHost = Kokkos::View<double*, Kokkos::HostSpace>("Diag for Seq SOR", count_total);
for(int i = 0; i < count_total; i++)
{
for(size_type j = ptrHost(i); j < ptrHost(i + 1); j++)
{
if(indHost(j) == i)
diagHost(i) = 1.0 / valHost(j);
}
}
}
auto xHost = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), x_vector);
auto yHost = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), y_vector);
if(use_sgs) {
timer.reset();
z = y_vector_t( "pcg::z" , count_total );
if (use_par_sgs) {
gauss_seidel_numeric
(&kh, count_total, count_total, crsMat.graph.row_map, crsMat.graph.entries, crsMat.values);
Space().fence();
precond_init_time += timer.seconds();
Space().fence();
timer.reset();
symmetric_gauss_seidel_apply
(&kh, count_total, count_total, crsMat.graph.row_map, crsMat.graph.entries, crsMat.values, z, r, true, true, 1.0, apply_count);
Space().fence();
}
else if(use_sequential_sgs) {
//z = LHS (aka x), r RHS (aka y or b)
Kokkos::deep_copy(z, 0.0);
auto zhost = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), z);
auto rhost = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), r);
//as with par_sgs, init unknown to 0
timer.reset();
for(int sweep = 0; sweep < apply_count; sweep++)
{
KokkosSparse::Impl::Sequential::gaussSeidel<nnz_lno_t, size_type, double, double, double>
(count_total, // rows = cols of the matrix
1, // number of vectors in X and B
ptrHost.data(), indHost.data(), valHost.data(),
rhost.data(), count_total, //raw ptr to B vector, and B column stride (for when multiple RHS gets added to MTSGS)
zhost.data(), count_total, //raw ptr to X vector, and X column stride
diagHost.data(),
1.0,
"F");
KokkosSparse::Impl::Sequential::gaussSeidel<nnz_lno_t, size_type, double, double, double>
(count_total, 1,
ptrHost.data(), indHost.data(), valHost.data(),
rhost.data(), count_total,
zhost.data(), count_total,
diagHost.data(),
1.0,
"B");
}
//result is in z (but r doesn't change)
Kokkos::deep_copy(z, zhost);
Kokkos::deep_copy(r, rhost);
}
precond_time += timer.seconds();
precond_old_rdot = KokkosBlas::dot(r , z);
Kokkos::deep_copy(p, z);
}
iteration = 0 ;
#ifdef KK_TICTOCPRINT
std::cout << "norm_res:" << norm_res << " old_rdot:" << old_rdot<< std::endl;
#endif
while (tolerance < norm_res && iteration < maximum_iteration ) {
std::cout << "Running CG iteration " << iteration << ", current resnorm = " << norm_res << '\n';
timer.reset();
/* Ap = A * p */ KokkosSparse::spmv("N", 1, crsMat, pAll, 0, Ap);
Space().fence();
matvec_time += timer.seconds();
//const double pAp_dot = Kokkos::Example::all_reduce( dot( count_owned , p , Ap ) , import.comm );
//const double pAp_dot = dot<y_vector_t,y_vector_t, Space>( count_total , p , Ap ) ;
/* pAp_dot = dot(Ap , p ) */ const double pAp_dot = KokkosBlas::dot( p , Ap ) ;
double alpha = 0;
if (use_sgs){
alpha = precond_old_rdot / pAp_dot ;
}
else {
alpha = old_rdot / pAp_dot ;
}
/* x += alpha * p ; */ KokkosBlas::axpby(alpha, p, 1.0, x_vector);
/* r += -alpha * Ap ; */ KokkosBlas::axpby(-alpha, Ap, 1.0, r);
const double r_dot = KokkosBlas::dot( r , r );
const double beta_original = r_dot / old_rdot ;
double precond_r_dot = 1;
double precond_beta = 1;
if(use_sgs)
{
Space().fence();
timer.reset();
if (use_par_sgs)
{
symmetric_gauss_seidel_apply(
&kh,
count_total, count_total,
crsMat.graph.row_map,
crsMat.graph.entries,
crsMat.values, z, r, true, true,
1.0, apply_count);
}
else if(use_sequential_sgs)
{
//z = LHS (aka x), r RHS (aka y or b)
Kokkos::deep_copy(z, 0.0);
auto zhost = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), z);
auto rhost = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), r);
//as with the par_sgs version, init unknown (here, zhost) to 0
for(int sweep = 0; sweep < apply_count; sweep++)
{
KokkosSparse::Impl::Sequential::gaussSeidel<nnz_lno_t, size_type, double, double, double>
(count_total, 1,
ptrHost.data(), indHost.data(), valHost.data(),
rhost.data(), count_total,
zhost.data(), count_total,
diagHost.data(),
1.0,
"F");
KokkosSparse::Impl::Sequential::gaussSeidel<nnz_lno_t , size_type, double, double, double>
(count_total, 1,
ptrHost.data(), indHost.data(), valHost.data(),
rhost.data(), count_total,
zhost.data(), count_total,
diagHost.data(),
1.0,
"B");
}
Kokkos::deep_copy(z, zhost);
Kokkos::deep_copy(r, rhost);
}
precond_time += timer.seconds();
precond_r_dot = KokkosBlas::dot(r , z );
precond_beta = precond_r_dot / precond_old_rdot ;
}
double beta = 1;
if (!use_sgs) {
beta = beta_original;
/* p = r + beta * p ; */ KokkosBlas::axpby(1.0, r, beta, p);
}
else {
beta = precond_beta;
KokkosBlas::axpby(1.0, z, beta, p);
}
#ifdef KK_TICTOCPRINT
std::cout << "\tbeta_original:" << beta_original << std::endl;
if (use_sgs)
std::cout << "\tprecond_beta:" << precond_beta << std::endl;
#endif
norm_res = sqrt( old_rdot = r_dot );
precond_old_rdot = precond_r_dot;
#ifdef KK_TICTOCPRINT
std::cout << "\tnorm_res:" << norm_res << " old_rdot:" << old_rdot<< std::endl;
#endif
++iteration ;
}
Space().fence();
iter_time = wall_clock.seconds();
if ( 0 != result ) {
result->iteration = iteration ;
result->iter_time = iter_time ;
result->matvec_time = matvec_time ;
result->norm_res = norm_res ;
result->precond_time = precond_time;
result->precond_init_time = precond_init_time;
}
}
} // namespace Example
} // namespace Kokkos
}
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
#endif /* #ifndef KOKKOS_EXAMPLE_CG_SOLVE */