Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
IAlephCuda.h
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/* IAlephCnc.h (C) 2000-2012 */
9/* */
10/*---------------------------------------------------------------------------*/
11/*---------------------------------------------------------------------------*/
12#ifndef _ALEPH_INTERFACE_CNC_H_
13#define _ALEPH_INTERFACE_CNC_H_
14
15#include "arcane/aleph/cuda/AlephCuda.h"
16
17#include <cuda.h>
18#include <cublas.h>
19#include <cuda_runtime.h>
20
21/*---------------------------------------------------------------------------*/
22/*---------------------------------------------------------------------------*/
23
24namespace Arcane
25{
26/*---------------------------------------------------------------------------*/
27/*---------------------------------------------------------------------------*/
28
29/******************************************************************************
30 * AlephVectorCnc
31 *****************************************************************************/
32class AlephVectorCnc : public IAlephVector
33{
34 public:
35
36 AlephVectorCnc(ITraceMng* tm,
37 AlephKernel* kernel,
38 Integer index)
39 : IAlephVector(tm, kernel, index)
40 , m_cnc_vector(0)
41 {
42 debug() << "\t\t[AlephVectorCnc::AlephVectorCnc] new AlephVectorCnc";
43 }
44
45 /******************************************************************************
46 *****************************************************************************/
47 void AlephVectorCreate(void)
48 {
49 debug() << "\t\t[AlephVectorCnc::AlephVectorCreate] CNC VectorCreate 0-" << m_kernel->topology()->nb_row_size() - 1;
50 m_cnc_vector.allocate(m_kernel->topology()->nb_row_size());
51 debug() << "\t\t[AlephVectorCnc::AlephVectorCreate] done";
52 }
53
54 /******************************************************************************
55 *****************************************************************************/
56 void AlephVectorSet(const double* bfr_val, const int* bfr_idx, Integer size)
57 {
58 debug() << "\t\t[AlephVectorCnc::AlephVectorSet]";
59 for (Integer i = 0; i < size; ++i) {
60 m_cnc_vector[bfr_idx[i]] = bfr_val[i];
61 }
62 }
63
64 /******************************************************************************
65 *****************************************************************************/
66 int AlephVectorAssemble(void)
67 {
68 return 0;
69 }
70
71 /******************************************************************************
72 *****************************************************************************/
73 void AlephVectorGet(double* bfr_val, const int* bfr_idx, Integer size)
74 {
75 debug() << "\t\t[AlephVectorCnc::AlephVectorGet]";
76 for (Integer i = 0; i < size; ++i) {
77 bfr_val[i] = m_cnc_vector[bfr_idx[i]];
78 }
79 }
80
81 /******************************************************************************
82 *****************************************************************************/
83 void writeToFile(const String filename)
84 {
85 debug() << "\t\t[AlephVectorCnc::writeToFile]";
86 m_cnc_vector.print();
87 }
88
89 public:
90
91 CNC_Vector<double> m_cnc_vector;
92};
93
94/******************************************************************************
95 AlephMatrixCnc
96 *****************************************************************************/
97class AlephMatrixCnc : public IAlephMatrix
98{
99 public:
100
101 /******************************************************************************
102 AlephMatrixCnc
103 *****************************************************************************/
104 AlephMatrixCnc(ITraceMng* tm,
105 AlephKernel* kernel,
106 Integer index)
107 : IAlephMatrix(tm, kernel, index)
108 , m_cnc_matrix(0)
109 , m_cuda()
110 , m_smcrs()
111 , has_been_allocated_on_gpu(false)
112 {
113 debug() << "\t\t[AlephMatrixCnc::AlephMatrixCnc] new SolverMatrixCnc";
114 }
115
116 /******************************************************************************
117 * ~AlephMatrixCnc
118 *****************************************************************************/
119 ~AlephMatrixCnc()
120 {
121 debug() << "\t\t[AlephMatrixCnc::~AlephMatrixCnc] DELETE SolverMatrixCnc";
122 m_smcrs.gpu_deallocate();
123 }
124
125 /******************************************************************************
126 *****************************************************************************/
127 void AlephMatrixCreate(void)
128 {
129 debug() << "\t\t[AlephMatrixCnc::AlephMatrixCreate] CNC MatrixCreate";
130 m_cnc_matrix.allocate(m_kernel->topology()->nb_row_size(),
131 m_kernel->topology()->nb_row_size(),
132 CNC_Matrix::ROWS,
133 false); // symmetric ?
134 m_cuda.cnc_cuda_set_dim_vec_from_n(m_kernel->topology()->nb_row_size());
135 }
136
137 /******************************************************************************
138 *****************************************************************************/
139 void AlephMatrixSetFilled(bool) {}
140
141 /******************************************************************************
142 *****************************************************************************/
143 int AlephMatrixAssemble(void)
144 {
145 debug() << "\t\t[AlephMatrixCnc::AlephMatrixConfigure] AlephMatrixConfigure";
146 m_cuda.convert_matrix(m_cnc_matrix, m_smcrs, false);
147 if (has_been_allocated_on_gpu == false) {
148 m_smcrs.gpu_allocate();
149 has_been_allocated_on_gpu = true;
150 }
151 return 0;
152 }
153
154 /******************************************************************************
155 *****************************************************************************/
156 void AlephMatrixFill(int nrows, int* rows, int* cols, double* values)
157 {
158 debug() << "\t\t[AlephMatrixCnc::AlephMatrixFill]";
159 for (int i = 0, iMax = m_kernel->topology()->gathered_nb_setValued(m_kernel->rank()); i < iMax; ++i) {
160 m_cnc_matrix.add(rows[i],
161 cols[i],
162 values[i]);
163 }
164 debug() << "\t\t[AlephMatrixCnc::AlephMatrixFill] done";
165 }
166
167 /******************************************************************************
168 * LinftyNormVectorProductAndSub
169 *****************************************************************************/
170 Real LinftyNormVectorProductAndSub(AlephVector* x,
171 AlephVector* b)
172 {
173 throw FatalErrorException("LinftyNormVectorProductAndSub", "error");
174 }
175
176 /******************************************************************************
177 *****************************************************************************/
178 int AlephMatrixSolve(AlephVector* x,
179 AlephVector* b,
180 AlephVector* t,
181 Integer& nb_iteration,
182 Real* residual_norm,
183 AlephParams* solver_param)
184 {
185 const String func_name("SolverMatrixCnc::solve");
186
187 debug() << "\t\t[AlephMatrixCnc::AlephMatrixSolve] Getting X & B";
188 CNC_Vector<double>* X = &(dynamic_cast<AlephVectorCnc*>(x->implementation()))->m_cnc_vector;
189 CNC_Vector<double>* B = &(dynamic_cast<AlephVectorCnc*>(b->implementation()))->m_cnc_vector;
190
191 debug() << "\t\t[AlephMatrixCnc::AlephMatrixSolve] Flushing X";
192 X->set_all(0.0f);
193
194 // solving the algebraic system
195 unsigned int cnc_nb_iter_max = solver_param->maxIter();
196 double cnc_epsilon = solver_param->epsilon();
197
198 debug() << "\t\t[AlephMatrixCnc::AlephMatrixSolve] Launching CNC_Solver";
199 m_cuda.solve(m_smcrs, *B, *X, cnc_nb_iter_max, cnc_epsilon, nb_iteration, residual_norm);
200
201 /* debug()<<"\t\t[AlephMatrixCnc::AlephMatrixSolve] HARD-CODING arguments";
202 nb_iteration = cnc_nb_iter_max-1;
203 residual_norm[0] = cnc_epsilon;
204 */
205
206 if (nb_iteration == solver_param->maxIter() && solver_param->stopErrorStrategy()) {
207 info() << "\n============================================================";
208 info() << "\nThis error is returned after " << nb_iteration << "\n";
209 info() << "\nThe maximum number of solver iterations has been reached.";
210 info() << "\nIt is possible to ask the code not to consider this error.";
211 info() << "\nSee the dataset documentation regarding the solver service.";
212 info() << "\n======================================================";
213 throw Exception("AlephMatrixCnc::Solve", "The maximum number of solver iterations has been reached");
214 }
215 return 0;
216 }
217
218 /******************************************************************************
219 *****************************************************************************/
220 void writeToFile(const String filename)
221 {
222 //CNC_MatrixPrint(m_cnc_matrix, filename.localstr());
223 }
224
225 public:
226
227 CNC_Matrix m_cnc_matrix;
228 Cuda m_cuda;
229 CNC_MatrixCRS<double> m_smcrs;
230 bool has_been_allocated_on_gpu;
231};
232
233/*---------------------------------------------------------------------------*/
234/*---------------------------------------------------------------------------*/
235
236} // namespace Arcane
237
238/*---------------------------------------------------------------------------*/
239/*---------------------------------------------------------------------------*/
240
241#endif
Parameters of a linear system.
Definition AlephParams.h:32
Vector of a linear system.
Definition AlephVector.h:33
TraceMessageDbg debug(Trace::eDebugLevel=Trace::Medium) const
Flow for a debug message.
TraceMessage info() const
Flow for an information message.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Int32 Integer
Type representing an integer.
double Real
Type representing a real number.