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

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

merged trunk-jpl and trunk for revision 16135

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 ISSM_MPI_Status status;
114
115 /*recover my_rank:*/
116 my_rank=IssmComm::GetRank();
117 num_procs=IssmComm::GetSize();
118
119 /*First, figure out total number of rows combining all the cpus: */
120 ISSM_MPI_Reduce(&this->numrows,&total_numrows,1,ISSM_MPI_INT,ISSM_MPI_SUM,0,IssmComm::GetComm() );
121 ISSM_MPI_Bcast(&total_numrows,1,ISSM_MPI_INT,0,IssmComm::GetComm());
122
123 /*return if patch empty*/
124 if(total_numrows==0) return;
125
126 /*Now, allocate buffer to holds all the values, on node 0: */
127 if(my_rank==0)total_values=xNew<IssmDouble>(this->numcols*total_numrows);
128
129 /*Start by copying node 0 values onto total_values: */
130 if(my_rank==0){
131 count=0;
132 xMemCpy<IssmDouble>(total_values+count,this->values,this->numcols*this->numrows);
133 count+=this->numrows*this->numcols;
134 }
135
136 /*Now, ask other nodes to send their values: */
137 for(int i=1;i<num_procs;i++){
138 if (my_rank==i){
139 ISSM_MPI_Send(&this->numrows,1,ISSM_MPI_INT,0,1,IssmComm::GetComm());
140 if (this->numrows)ISSM_MPI_Send(this->values,this->numrows*this->numcols,ISSM_MPI_DOUBLE,0,1,IssmComm::GetComm());
141 }
142 if (my_rank==0){
143 ISSM_MPI_Recv(&node_numrows,1,ISSM_MPI_INT,i,1,IssmComm::GetComm(),&status);
144 if (node_numrows)ISSM_MPI_Recv(total_values+count,node_numrows*this->numcols,ISSM_MPI_DOUBLE,i,1,IssmComm::GetComm(),&status);
145 count+=node_numrows*this->numcols;
146 }
147 }
148
149 /*Now, node 0 has total_values, of size total_numrows*this->numcols. Update the fields in the patch, to reflect this new
150 * reality. For other cpus, no point in keeping their data anymore: */
151 if(my_rank==0){
152 this->numrows=total_numrows;
153 xDelete<IssmDouble>(this->values);
154 this->values=total_values;
155 }
156 else{
157 this->numrows=0;
158 xDelete<IssmDouble>(this->values);
159 }
160}/*}}}*/
Note: See TracBrowser for help on using the repository browser.