source: issm/trunk/src/c/classes/Patch.cpp@ 15396

Last change on this file since 15396 was 15396, checked in by Mathieu Morlighem, 12 years ago

merged trunk-jpl and trunk for revision 15394

File size: 4.3 KB
Line 
1/*!\file Patch.c
2 * \brief: implementation of the Patch object
3 */
4
5/*Include files */
6#ifdef HAVE_CONFIG_H
7 #include <config.h>
8#else
9#error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
10#endif
11
12#include <stdio.h>
13#include <string.h>
14#include <math.h>
15#include "./classes.h"
16#include "../shared/shared.h"
17
18/*Object constructors and destructors:*/
19/*FUNCTION Patch::Patch() default constructor {{{*/
20Patch::Patch(){
21 this->numrows = 0;
22 this->numcols = 0;
23 this->maxvertices = 0;
24 this->maxnodes = 0;
25 this->values = NULL;
26}
27/*}}}*/
28/*FUNCTION Patch::Patch(int numrows, int maxvertices, int maxnodes){{{*/
29Patch::Patch(int in_numrows, int in_maxvertices, int in_maxnodes){
30
31 this->numrows=in_numrows;
32 this->maxvertices=in_maxvertices;
33 this->maxnodes=in_maxnodes;
34 this->numcols=1 //enum_type
35 +1 //step
36 +1 //time
37 +1 //element id
38 +1 //interpolation type
39 +maxvertices //vertices
40 +maxnodes; //nodes
41
42 //Allocate values and fill with NaN:
43 if (this->numcols*this->numrows==0){
44 this->values=NULL;
45 }
46 else{
47 this->values=xNew<IssmDouble>(this->numcols*this->numrows);
48 for(int i=0;i<this->numrows;i++){
49 for(int j=0;j<this->numcols;j++){
50 this->values[i*this->numcols+j]=NAN;
51 }
52 }
53 }
54
55}
56/*}}}*/
57/*FUNCTION Patch::~Patch(){{{*/
58Patch::~Patch(){
59 xDelete<IssmDouble>(values);
60}
61/*}}}*/
62
63/*Object methods*/
64/*FUNCTION Patch::fillelementinfo{{{*/
65void Patch::fillelementinfo(int count, int element_id, int* vertices_ids, int num_vertices){
66
67 int i;
68 IssmDouble* row=NULL;
69
70 /*point to the start of the row: */
71 row=this->values+count*this->numcols;
72
73 /*Let's remember what is on a row:
74 enum_type step time element_id interpolation vertices_ids nodal_values
75 */
76 row[3]=element_id;
77 for(i=0;i<num_vertices;i++){
78 row[5+i]=vertices_ids[i];
79 }
80
81}
82/*}}}*/
83/*FUNCTION Patch::fillresultinfo{{{*/
84void Patch::fillresultinfo(int count,int enum_type,int step, IssmDouble time, int interpolation, IssmDouble* nodal_values, int num_nodes){
85
86 int i;
87 IssmDouble* row=NULL;
88
89 /*point to the start of the row: */
90 row=this->values+count*this->numcols;
91
92 /*Let's remember what is on a row:
93 enum_type step time element_id interpolation vertices_ids nodal_values */
94 row[0]=enum_type;
95 row[1]=(IssmDouble)step;
96 row[2]=time;
97 row[4]=interpolation;
98 for(i=0;i<num_nodes;i++){
99 row[5+this->maxvertices+i]=nodal_values[i];
100 }
101
102}
103/*}}}*/
104/*FUNCTION Patch::Gather{{{*/
105void Patch::Gather(void){
106
107 int count;
108 int my_rank;
109 int num_procs;
110 int total_numrows;
111 int node_numrows;
112 IssmDouble *total_values = NULL;
113 #ifdef _HAVE_MPI_
114 MPI_Status status;
115 #endif
116
117 /*recover my_rank:*/
118 my_rank=IssmComm::GetRank();
119 num_procs=IssmComm::GetSize();
120
121 /*First, figure out total number of rows combining all the cpus: */
122 #ifdef _HAVE_MPI_
123 MPI_Reduce(&this->numrows,&total_numrows,1,MPI_INT,MPI_SUM,0,IssmComm::GetComm() );
124 MPI_Bcast(&total_numrows,1,MPI_INT,0,IssmComm::GetComm());
125 #else
126 total_numrows=this->numrows;
127 #endif
128
129 /*return if patch empty*/
130 if(total_numrows==0) return;
131
132 /*Now, allocate buffer to holds all the values, on node 0: */
133 if(my_rank==0)total_values=xNew<IssmDouble>(this->numcols*total_numrows);
134
135 /*Start by copying node 0 values onto total_values: */
136 if(my_rank==0){
137 count=0;
138 xMemCpy<IssmDouble>(total_values+count,this->values,this->numcols*this->numrows);
139 count+=this->numrows*this->numcols;
140 }
141
142 /*Now, ask other nodes to send their values: */
143 #ifdef _HAVE_MPI_
144 for(int i=1;i<num_procs;i++){
145 if (my_rank==i){
146 MPI_Send(&this->numrows,1,MPI_INT,0,1,IssmComm::GetComm());
147 if (this->numrows)MPI_Send(this->values,this->numrows*this->numcols,MPI_DOUBLE,0,1,IssmComm::GetComm());
148 }
149 if (my_rank==0){
150 MPI_Recv(&node_numrows,1,MPI_INT,i,1,IssmComm::GetComm(),&status);
151 if (node_numrows)MPI_Recv(total_values+count,node_numrows*this->numcols,MPI_DOUBLE,i,1,IssmComm::GetComm(),&status);
152 count+=node_numrows*this->numcols;
153 }
154 }
155 #endif
156
157 /*Now, node 0 has total_values, of size total_numrows*this->numcols. Update the fields in the patch, to reflect this new
158 * reality. For other cpus, no point in keeping their data anymore: */
159 if(my_rank==0){
160 this->numrows=total_numrows;
161 xDelete<IssmDouble>(this->values);
162 this->values=total_values;
163 }
164 #ifdef _HAVE_MPI_
165 else{
166 this->numrows=0;
167 xDelete<IssmDouble>(this->values);
168 }
169 #endif
170}/*}}}*/
Note: See TracBrowser for help on using the repository browser.