10#include "arcane/IMesh.h"
11#include "arcane/IApplication.h"
12#include "arcane/IParallelMng.h"
13#include "arcane/IVariableMng.h"
14#include "arcane/utils/ITraceMng.h"
15#include "arcane/IVariableAccessor.h"
16#include "arcane/FactoryService.h"
17#include "arcane/ServiceFinder2.h"
18#include "arcane/SharedVariable.h"
19#include "arcane/CommonVariables.h"
20#include "arcane/utils/ScopedPtr.h"
21#include "arcane/AbstractService.h"
22#include "arcane/VariableCollection.h"
24#include "arcane/utils/PlatformUtils.h"
25#include "arcane/utils/IOnlineDebuggerService.h"
26#include "arcane/ITransferValuesParallelOperation.h"
29#define GL_MULTISAMPLE 0x809D
32#include "arcane/hyoda/HyodaIceTGlobal.h"
33#include "arcane/hyoda/Hyoda.h"
34#include "arcane/hyoda/HyodaArc.h"
35#include "arcane/hyoda/HyodaTcp.h"
36#include "arcane/hyoda/HyodaIceT.h"
37#include "arcane/hyoda/HyodaMatrix.h"
38#include "arcane/hyoda/HyodaMix.h"
39extern void ppmWrite(
const char*,
const unsigned char*,
int width,
int height);
44 void drawGLCallback(
void){
45 if (!HyodaMatrixCallbackHandle)
return;
46 HyodaMatrixCallbackHandle->drawGL();
63HyodaMatrix::HyodaMatrix(Hyoda *hd,
68 HyodaTcp *_tcp): TraceAccessor(tm),
72 icetCommunicator(NULL),
74 rank(sd->parallelMng()->commRank()),
75 num_proc(sd->parallelMng()->commSize()),
90 debug()<<
"\33[35m[HyodaMatrix::HyodaMatrix] New "
91 <<
", screen_width="<<m_screen_width
92 <<
", screen_height="<<m_screen_height
95 m_hyoda_matrix_n=(
int*)calloc(8,
sizeof(
int));
96 m_hyoda_matrix_i=(
int**)calloc(8,
sizeof(
int*));
97 m_hyoda_matrix_j=(
int**)calloc(8,
sizeof(
int*));
98 m_hyoda_matrix_val=(
double**)calloc(8,
sizeof(
double*));
108 osMesaContext = OSMesaCreateContextExt(OSMESA_RGBA, 8, 0, 0, NULL);
109 if (!osMesaContext) fatal()<<
"\33[35mOSMesaCreateContext failed!\33[m\n";
111 m_image_buffer=malloc(renderSize());
112 if (!m_image_buffer) fatal()<<
"\33[35mAlloc image failed!\33[m\n";
114 if (!OSMesaMakeCurrent(osMesaContext,
119 fatal()<<
"\33[35mOSMesaMakeCurrent failed!\33[m\n";
122 glGetIntegerv(GL_DEPTH_BITS, &z);
123 glGetIntegerv(GL_STENCIL_BITS, &s);
124 glGetIntegerv(GL_ACCUM_RED_BITS, &a);
125 debug()<<
"\33[35m[HyodaMatrix] Depth="<<z<<
", Stencil="<<s<<
", Accum="<<a<<
"\33[m";
128 #warning IceT needs MPI_Init
130 if (sd->parallelMng()->isParallel()){
131 debug() <<
"\33[35m[HyodaMatrix] isParallel icetCreateMPICommunicator @"
132 << sd->parallelMng()->getMPICommunicator()<<
"\33[m";
133 icetCommunicator = icetCreateMPICommunicator(*(MPI_Comm*)sd->parallelMng()->getMPICommunicator());
135 debug()<<
"\33[35m[HyodaMatrix] MPI_COMM_SELF icetCreateMPICommunicator\33[m";
136 icetCommunicator = icetCreateMPICommunicator(MPI_COMM_SELF);
138 icetContext = icetCreateContext(icetCommunicator);
142 icetGLDrawCallback(drawGLCallback);
143 HyodaMatrixCallbackHandle=
this;
145 icetAddTile(0, 0, m_screen_width, m_screen_height, 0);
146 icetStrategy(ICET_STRATEGY_SEQUENTIAL);
147 icetSingleImageStrategy(ICET_SINGLE_IMAGE_STRATEGY_AUTOMATIC);
149 icetSetColorFormat(ICET_IMAGE_COLOR_RGBA_UBYTE);
150 icetSetDepthFormat(ICET_IMAGE_DEPTH_FLOAT);
151 icetCompositeMode(ICET_COMPOSITE_MODE_Z_BUFFER);
152 icetBoundingBoxd(-1.0f, +1.0f, -1.0f, +1.0f, -4.0f, 16.0f);
153 setLeftRightBottomTop();
154 icetDisable(ICET_FLOATING_VIEWPORT);
155 icetEnable(ICET_ORDERED_COMPOSITE);
158 debug()<<
"\33[35m[HyodaMatrix] checkOglError\33[m";
167void HyodaMatrix::setIJVal(
int cpu,
int n,
int *i,
int *j,
double *val){
168 m_hyoda_matrix_cpu=math::max(cpu,m_hyoda_matrix_cpu);
169 m_hyoda_matrix_n[cpu]=n;
170 m_hyoda_matrix_i[cpu]=i;
171 m_hyoda_matrix_j[cpu]=j;
172 m_hyoda_matrix_val[cpu]=val;
173 debug()<<
"\33[7m[HyodaMatrix::setIJVal] cpu="<<cpu<<
",n="<<n
174 <<
", m_hyoda_matrix_cpu="<<m_hyoda_matrix_cpu<<
"\33[m";
181void HyodaMatrix::setLeftRightBottomTop(){
182 lrbtnf[0]=lrbtnf[1]=lrbtnf[2]=lrbtnf[3]=0.0;
186 int min_i=+123456789;
187 int max_i=-123456789;
188 int min_j=+123456789;
189 int max_j=-123456789;
191 for(
int cpu=0;cpu<=m_hyoda_matrix_cpu;cpu+=1){
192 debug()<<
"\33[7m[HyodaMatrix::setLeftRightBottomTop] cpu="<<cpu<<
"/"<<m_hyoda_matrix_cpu
193 <<
", m_hyoda_matrix_n[cpu]="<<m_hyoda_matrix_n[cpu]<<
"\33[m";
194 for(
int k=0;k<m_hyoda_matrix_n[cpu];k+=1){
195 min_i = math::min(min_i,m_hyoda_matrix_i[cpu][k]);
196 max_i = math::max(max_i,m_hyoda_matrix_i[cpu][k]);
197 min_j = math::min(min_j,m_hyoda_matrix_j[cpu][k]);
198 max_j = math::max(max_j,m_hyoda_matrix_j[cpu][k]);
205 debug()<<
"\33[35m[HyodaMatrix::setLeftRightBottomTop] min_i="<< min_i<<
"\33[m";
206 debug()<<
"\33[35m[HyodaMatrix::setLeftRightBottomTop] max_i="<< max_i<<
"\33[m";
207 debug()<<
"\33[35m[HyodaMatrix::setLeftRightBottomTop] min_j="<< min_j<<
"\33[m";
208 debug()<<
"\33[35m[HyodaMatrix::setLeftRightBottomTop] max_j="<< max_j<<
"\33[m";
210 UniqueArray<Real> rdcMin(3);
211 UniqueArray<Real> rdcMax(3);
215 m_sub_domain->parallelMng()->reduce(Parallel::ReduceMin,rdcMin.view());
219 m_sub_domain->parallelMng()->reduce(Parallel::ReduceMax,rdcMax.view());
226 debug()<<
"\33[35m[HyodaMatrix::setLeftRightBottomTop] left =" << lrbtnf[0]<<
"\33[m";
227 debug()<<
"\33[35m[HyodaMatrix::setLeftRightBottomTop] right =" << lrbtnf[1]<<
"\33[m";
228 debug()<<
"\33[35m[HyodaMatrix::setLeftRightBottomTop] bottom =" << lrbtnf[2]<<
"\33[m";
229 debug()<<
"\33[35m[HyodaMatrix::setLeftRightBottomTop] top =" << lrbtnf[3]<<
"\33[m";
230 debug()<<
"\33[35m[HyodaMatrix::setLeftRightBottomTop] near =" << lrbtnf[4]<<
"\33[m";
231 debug()<<
"\33[35m[HyodaMatrix::setLeftRightBottomTop] far =" << lrbtnf[5]<<
"\33[m";
238void HyodaMatrix::sxyzip(
double *pov){
243 debug()<<
"\33[35m[HyodaMatrix::sxyzi] scale="<<scale<<
244 ", rot_x="<<rot_x<<
", rot_y="<<rot_y<<
", rot_z="<<rot_z<<
"\33[m";
251int HyodaMatrix::renderSize(
void){
252 return m_screen_width*m_screen_height*4*
sizeof(GLubyte);
259HyodaMatrix::~HyodaMatrix(){
260 free(m_image_buffer);
263 debug()<<
"\33[35m~HyodaMatrix"<<
"\33[m";
264 icetDestroyContext(icetContext);
265 OSMesaDestroyContext(osMesaContext);
273void HyodaMatrix::render(
void){
274 debug()<<
"\33[35m[HyodaMatrix] render\33[m";
275 icetGLDrawCallback(drawGLCallback);
278 platform::enableFloatingException(
false);
279 IceTImage image=icetGLDrawFrame();
280 platform::enableFloatingException(
true);
284 m_sub_domain->parallelMng()->barrier();
289 unsigned char* imgAddress=icetImageGetColorub(image);
292 if (!platform::getEnvironmentVariable(
"ARCANE_HYODA_MATRIX_PPM").null()){
294 snprintf(filename,3883,
"/tmp/HyODA_Mtx_%04d.ppm",
295 m_sub_domain->commonVariables().globalIteration());
296 debug()<<
"\33[33m[HyodaIceT::render] \33[33mARCANE_HYODA_MATRIX_PPM\33[m "
297 << icetImageGetWidth(image) <<
"x" << icetImageGetHeight(image)
298 <<
" image " << filename;
299 ppmWrite(filename, imgAddress,(
int)m_screen_width, (
int)m_screen_height);
302 debug()<<
"\33[35m[HyodaMatrix::render] sending header packet for image!\33[m";
303 char icePacketHeader[8];
305 *(
unsigned int*)&icePacketHeader[0]=0x78f78f67ul;
307 *(
unsigned int*)&icePacketHeader[4]=8;
308 m_tcp->send(icePacketHeader,8);
309 m_tcp->waitForAcknowledgment();
310 debug()<<
"\33[35m[HyodaMatrix::render] sending image packet!, renderSize="<<renderSize()<<
"\33[m";
311 m_tcp->send((
void*)imgAddress, renderSize());
312 m_tcp->waitForAcknowledgment();
313 debug()<<
"\33[35m[HyodaMatrix::render] Waiting for POV answer!\33[m";
314 m_tcp->recvPov(m_pov_sxyzip);
317 m_sub_domain->parallelMng()->barrier();
320 UniqueArray<Real> pov_to_broadcast(0);
321 pov_to_broadcast.add(m_pov_sxyzip[0]);
322 pov_to_broadcast.add(m_pov_sxyzip[1]);
323 pov_to_broadcast.add(m_pov_sxyzip[2]);
324 pov_to_broadcast.add(m_pov_sxyzip[3]);
325 pov_to_broadcast.add(m_pov_sxyzip[4]);
326 pov_to_broadcast.add(m_pov_sxyzip[5]);
327 debug()<<
"\33[35m[Hyoda::hook] broadcasting pov..."<<
"\33[m";
328 m_sub_domain->parallelMng()->broadcast(pov_to_broadcast.view(),0);
329 m_pov_sxyzip[0]=pov_to_broadcast[0];
330 m_pov_sxyzip[1]=pov_to_broadcast[1];
331 m_pov_sxyzip[2]=pov_to_broadcast[2];
332 m_pov_sxyzip[3]=pov_to_broadcast[3];
333 m_pov_sxyzip[4]=pov_to_broadcast[4];
334 m_pov_sxyzip[5]=pov_to_broadcast[5];
335 sxyzip(m_pov_sxyzip);
336 debug()<<
"\33[35m[Hyoda::hook] pov done"<<
"\33[m";
337 debug()<<
"\33[35m[HyodaMatrix::render] sxyzip:"
338 <<
"scale=" << m_pov_sxyzip[0]
339 <<
", x=" << m_pov_sxyzip[1]
340 <<
", y=" << m_pov_sxyzip[2]
341 <<
", z=" << m_pov_sxyzip[3]
342 <<
", i=" << m_pov_sxyzip[4]
343 <<
", p=" << m_pov_sxyzip[5]
351void HyodaMatrix::initGL(
void){
352 debug()<<
"\33[35m[HyodaMatrix] initGL in"<<
"\33[m";
354 glViewport(0, 0, m_screen_width,m_screen_height);
356 glEnable(GL_DEPTH_TEST);
357 glEnable(GL_MULTISAMPLE);
358 glEnable(GL_POINT_SMOOTH);
363 glDepthFunc(GL_LESS);
364 glShadeModel(GL_SMOOTH);
365 glShadeModel(GL_FLAT);
366 glLineStipple(1, 0x0101);
367 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
369 glColor3f(0.0f, 0.0f, 0.0f);
370 glClearColor(0.0, 0.0, 0.0, 0.0);
371 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
372 debug()<<
"\33[35m[HyodaMatrix] initGL out"<<
"\33[m";
380void HyodaMatrix::drawGL(
void){
381 debug()<<
"\33[35m[HyodaMatrix] drawGL"<<
"\33[m";
383 glShadeModel(GL_SMOOTH);
384 glMatrixMode(GL_PROJECTION);
387 setLeftRightBottomTop();
389 Real length=lrbtnf[1]-lrbtnf[0];
390 Real height=lrbtnf[3]-lrbtnf[2];
392 glOrtho(lrbtnf[0]-length/4., lrbtnf[1]+length/4., lrbtnf[2]-height/4., lrbtnf[3]+height/4., lrbtnf[4], lrbtnf[5]);
396 glRotatef(rot_x, 1.0f, 0.0f, 0.0f);
397 glRotatef(rot_y, 0.0f, 1.0f, 0.0f);
398 glRotatef(rot_z, 0.0f, 0.0f, 1.0f);
399 glScalef(scale, scale, scale);
400 glMatrixMode(GL_MODELVIEW);
401 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
412static inline void glVertex(Real3 p){
416inline static int glQuadFull(Real3 p0, Real3 p1, Real3 p2, Real3 p3, Real3 rgb){
417 glColor3d(rgb.x, rgb.y, rgb.z);
440void HyodaMatrix::drawMatrix(
void){
441 debug()<<
"\33[35m[HyodaMatrix] drawMatrix"<<
"\33[m";
443 Real imin=+HUGE_VAL;
Real imax=-HUGE_VAL;
444 Real jmin=+HUGE_VAL;
Real jmax=-HUGE_VAL;
445 Real cmin=+HUGE_VAL;
Real cmax=-HUGE_VAL;
447 for(
int cpu=0;cpu<=m_hyoda_matrix_cpu;cpu+=1){
448 iceRowMinMax(cpu,imin,imax);
449 iceColMinMax(cpu,jmin,jmax);
450 iceValMinMax(cpu,cmin,cmax);
451 n+=m_hyoda_matrix_n[cpu];
453 debug()<<
"\33[35m[HyodaMatrix] imin="<<imin<<
", imax="<<imax<<
"\33[m";
454 debug()<<
"\33[35m[HyodaMatrix] jmin="<<jmin<<
", jmax="<<jmax<<
"\33[m";
455 debug()<<
"\33[35m[HyodaMatrix] cmin="<<cmin<<
", cmax="<<cmax<<
"\33[m";
456 debug()<<
"\33[35m[HyodaMatrix] n="<<n<<
"\33[m";
457 for(
int cpu=0;cpu<=m_hyoda_matrix_cpu;cpu+=1){
458 for(
int k=0;k<m_hyoda_matrix_n[cpu];k+=1){
459 const double value=m_hyoda_matrix_j[cpu][k];
460 const Real3 p(imax-m_hyoda_matrix_i[cpu][k],m_hyoda_matrix_j[cpu][k],0);
461 const Real3 n0=p+Real3(-0.5,-0.5,0.0);
462 const Real3 n1=p+Real3(+0.5,-0.5,0.0);
463 const Real3 n2=p+Real3(+0.5,+0.5,0.0);
464 const Real3 n3=p+Real3(-0.5,+0.5,0.0);
466 setColor(cmin,cmax,value,rgb);
467 glQuadFull(n0,n1,n2,n3,rgb);
478void HyodaMatrix::iceValMinMax(
int cpu,
Real &min,
Real &max){
479 for(
int k=0;k<m_hyoda_matrix_n[cpu];k+=1){
480 const Real val=m_hyoda_matrix_j[cpu][k];
481 min = math::min(min,val);
482 max = math::max(max,val);
486 debug()<<
"\33[35m[iceValMinMax] cpu="<<cpu<<
", min="<<
min<<
", max="<<
max<<
"\33[m";
490void HyodaMatrix::iceRowMinMax(
int cpu,
Real &min,
Real &max){
491 for(
int k=0;k<m_hyoda_matrix_n[cpu];k+=1){
492 const Real val=m_hyoda_matrix_i[cpu][k];
493 min = math::min(min,val);
494 max = math::max(max,val);
498 debug()<<
"\33[35m[iceRowMinMax] cpu="<<cpu<<
", min="<<
min<<
", max="<<
max<<
"\33[m";
501void HyodaMatrix::iceColMinMax(
int cpu,
Real &min,
Real &max){
502 for(
int k=0;k<m_hyoda_matrix_n[cpu];k+=1){
503 const Real val=m_hyoda_matrix_j[cpu][k];
504 min = math::min(min,val);
505 max = math::max(max,val);
509 debug()<<
"\33[35m[iceColMinMax] cpu="<<cpu<<
", min="<<
min<<
", max="<<
max<<
"\33[m";
518setColor(
double min,
double max,
double v, Real3 &rgb)
520 ARCANE_ASSERT(min<=max,(
"setColor min<=max"));
526 ARCANE_ASSERT(mid!=0.,(
"setColor mid!=0."));
527 rgb.x=rgb.y=rgb.z=1.0;
530 if (v<(min+0.25*mid)){
532 rgb.y=4.*(v-
min)/mid;
533 }
else if (v<(min+0.5*mid)){
535 rgb.z=1.+4*(
min+0.25*mid-v)/mid;
536 }
else if (v<(min+0.75*mid)){
537 rgb.x=4*(v-
min-0.5*mid)/mid;
540 rgb.y=1.+4*(
min+0.75*mid-v)/mid;
549void HyodaMatrix::checkIceTError(
void){
550#define CASE_ERROR(ename) case ename: debug() << "## IceT status = \33[35m" << #ename << "\33[m"; break;
551 switch (icetGetError()) {
552 case(ICET_NO_ERROR):
break;
553 CASE_ERROR(ICET_SANITY_CHECK_FAIL);
554 CASE_ERROR(ICET_INVALID_ENUM);
555 CASE_ERROR(ICET_BAD_CAST);
556 CASE_ERROR(ICET_OUT_OF_MEMORY);
557 CASE_ERROR(ICET_INVALID_OPERATION);
558 CASE_ERROR(ICET_INVALID_VALUE);
559 default:debug()<<
"\33[35m## UNKNOWN ICET ERROR CODE!!!!!\33[m";
568void HyodaMatrix::checkOglError(
void){
569#define CASE_ERROR(ename) case ename: debug() << "\33[35m## OpenGL status = " << #ename << "\33[m"; break;
570 switch (glGetError()) {
571 case(GL_NO_ERROR):
break;
572 CASE_ERROR(GL_INVALID_ENUM);
573 CASE_ERROR(GL_INVALID_VALUE);
574 CASE_ERROR(GL_INVALID_OPERATION);
575 CASE_ERROR(GL_STACK_OVERFLOW);
576 CASE_ERROR(GL_STACK_UNDERFLOW);
577 CASE_ERROR(GL_OUT_OF_MEMORY);
578#ifdef GL_TABLE_TOO_LARGE
579 CASE_ERROR(GL_TABLE_TOO_LARGE);
581 default:debug()<<
"\33[35m## UNKNOWN GL ERROR CODE!!!!!\33[m";
Fichier de configuration d'Arcane.
Lecteur des fichiers de maillage via la bibliothèque LIMA.
T max(const T &a, const T &b, const T &c)
Retourne le maximum de trois éléments.
ARCCORE_HOST_DEVICE Real2 min(Real2 a, Real2 b)
Retourne le minimum de deux Real2.