~ubuntu-branches/ubuntu/precise/code-saturne/precise

« back to all changes in this revision

Viewing changes to salome/cfd_proxy/cfd_proxy_forward.c

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2011-11-24 00:00:08 UTC
  • mfrom: (6.1.9 sid)
  • Revision ID: package-import@ubuntu.com-20111124000008-2vo99e38267942q5
Tags: 2.1.0-3
Install a missing file

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//============================================================================
 
2
// Forwarding of function calls
 
3
//============================================================================
 
4
 
 
5
/*
 
6
  This file is part of Code_Saturne, a general-purpose CFD tool.
 
7
 
 
8
  Copyright (C) 1998-2011 EDF S.A.
 
9
 
 
10
  This program is free software; you can redistribute it and/or modify it under
 
11
  the terms of the GNU General Public License as published by the Free Software
 
12
  Foundation; either version 2 of the License, or (at your option) any later
 
13
  version.
 
14
 
 
15
  This program is distributed in the hope that it will be useful, but WITHOUT
 
16
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
17
  FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 
18
  details.
 
19
 
 
20
  You should have received a copy of the GNU General Public License along with
 
21
  this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
 
22
  Street, Fifth Floor, Boston, MA 02110-1301, USA.
 
23
*/
 
24
 
 
25
#include "cs_config.h"
 
26
 
 
27
// System and SALOME headers
 
28
 
 
29
#include <assert.h>
 
30
#include <errno.h>
 
31
#include <stdio.h>
 
32
#include <stdlib.h>
 
33
#include <string.h>
 
34
#include <time.h>
 
35
 
 
36
#include <calcium.h>
 
37
 
 
38
// Local headers
 
39
 
 
40
#include "cfd_proxy_defs.h"
 
41
#include "cfd_proxy_comm.h"
 
42
#include "cfd_proxy_forward.h"
 
43
 
 
44
//----------------------------------------------------------------------------
 
45
 
 
46
#ifdef __cplusplus
 
47
extern "C" {
 
48
#if 0
 
49
} /* Fake brace to force back Emacs auto-indentation back to column 0 */
 
50
#endif
 
51
#endif /* __cplusplus */
 
52
 
 
53
//============================================================================
 
54
//  Constants and Macros
 
55
//============================================================================
 
56
 
 
57
//============================================================================
 
58
// Structure definition
 
59
//============================================================================
 
60
 
 
61
//============================================================================
 
62
// Global variables
 
63
//============================================================================
 
64
 
 
65
//============================================================================
 
66
// Private function definitions
 
67
//============================================================================
 
68
 
 
69
//----------------------------------------------------------------------------
 
70
// Forward a CALCIUM write call
 
71
//
 
72
// parameters:
 
73
//   comm   <-> communicator
 
74
//   r      <-> container for temporary communication request data
 
75
//   type   <-- data type
 
76
//
 
77
// returns:
 
78
//   0 if request was forwarded correctly, -1 for end-of-file, -2 for error
 
79
//----------------------------------------------------------------------------
 
80
 
 
81
static int
 
82
_forward_calcium_write(cfd_proxy_comm_t          *comm,
 
83
                       cfd_proxy_comm_request_t  *r,
 
84
                       cfd_proxy_type_t           type)
 
85
{
 
86
  void *component = cfd_proxy_glob_component[r->comp_id];
 
87
 
 
88
  int time_dep = r->int_vals[0];
 
89
  int iteration = r->int_vals[1];
 
90
  int n_vals = r->int_vals[2];
 
91
  double cur_time = r->double_vals[0];
 
92
  float _cur_time = cur_time;
 
93
  char *var_name = r->string_vals;
 
94
 
 
95
  size_t type_size = cfd_proxy_glob_type_size[type];
 
96
 
 
97
  int comm_retval = 0, retval = 0;
 
98
  char *buffer = NULL;
 
99
 
 
100
  if (n_vals > 0) {
 
101
    CFDP_MALLOC(buffer, n_vals*type_size, char);
 
102
    comm_retval = cfd_proxy_comm_read(comm, buffer, type_size, n_vals);
 
103
    if (comm_retval != 0) {
 
104
      CFDP_FREE(buffer);
 
105
      return comm_retval;
 
106
    }
 
107
  }
 
108
 
 
109
  switch(type) {
 
110
  case CFD_PROXY_TYPE_int:
 
111
    retval = cp_een(component, time_dep, _cur_time, iteration,
 
112
                    var_name, n_vals, (int *)buffer);
 
113
    break;
 
114
  case CFD_PROXY_TYPE_float:
 
115
    retval = cp_ere(component, time_dep, _cur_time, iteration,
 
116
                    var_name, n_vals, (float *)buffer);
 
117
    break;
 
118
  case CFD_PROXY_TYPE_double:
 
119
    retval = cp_edb(component, time_dep, cur_time, iteration,
 
120
                    var_name, n_vals, (double *)buffer);
 
121
    break;
 
122
  default:
 
123
    assert(0);
 
124
  }
 
125
 
 
126
  comm_retval = cfd_proxy_comm_write_response(comm, retval, 0, 0, 0,
 
127
                                              NULL, NULL, NULL);
 
128
 
 
129
  if (n_vals > 0)
 
130
    CFDP_FREE(buffer);
 
131
 
 
132
  return comm_retval;
 
133
}
 
134
 
 
135
//----------------------------------------------------------------------------
 
136
// Forward a CALCIUM read call
 
137
//
 
138
// parameters:
 
139
//   comm     <-> communicator
 
140
//   r        <-> container for temporary communication request data
 
141
//   type     <-- data type
 
142
//
 
143
// returns:
 
144
//   0 if request was forwarded correctly, -2 for error
 
145
//----------------------------------------------------------------------------
 
146
 
 
147
static int
 
148
_forward_calcium_read(cfd_proxy_comm_t          *comm,
 
149
                      cfd_proxy_comm_request_t  *r,
 
150
                      cfd_proxy_type_t           type)
 
151
{
 
152
  void *component = cfd_proxy_glob_component[r->comp_id];
 
153
 
 
154
  int time_dep = r->int_vals[0];
 
155
  int iteration = r->int_vals[1];
 
156
  int n_vals_max = r->int_vals[2];
 
157
  int n_vals = 0;
 
158
  double min_time = r->double_vals[0];
 
159
  double max_time = r->double_vals[1];
 
160
  float _min_time = min_time;
 
161
  float _max_time = max_time;
 
162
  char *var_name = r->string_vals;
 
163
 
 
164
  int int_out[2];
 
165
  double double_out[1];
 
166
 
 
167
  size_t type_size = cfd_proxy_glob_type_size[type];
 
168
 
 
169
  int comm_retval = 0;
 
170
  int retval = 0;
 
171
  char *buffer = NULL;
 
172
 
 
173
  if (n_vals_max > 0)
 
174
    CFDP_MALLOC(buffer, n_vals_max*type_size, char);
 
175
 
 
176
  switch(type) {
 
177
  case CFD_PROXY_TYPE_int:
 
178
    retval = cp_len(component, time_dep, &_min_time, &_max_time, &iteration,
 
179
                    var_name, n_vals_max, &n_vals, (int *)buffer);
 
180
    min_time = _min_time;
 
181
    break;
 
182
  case CFD_PROXY_TYPE_float:
 
183
    retval = cp_lre(component, time_dep, &_min_time, &_max_time, &iteration,
 
184
                    var_name, n_vals_max, &n_vals, (float *)buffer);
 
185
    min_time = _min_time;
 
186
    break;
 
187
  case CFD_PROXY_TYPE_double:
 
188
    retval = cp_ldb(component, time_dep, &min_time, &max_time, &iteration,
 
189
                    var_name, n_vals_max, &n_vals, (double *)buffer);
 
190
    break;
 
191
  default:
 
192
    assert(0);
 
193
  }
 
194
 
 
195
  int_out[0] = iteration;
 
196
  int_out[1] = n_vals;
 
197
  double_out[0] = min_time;
 
198
 
 
199
  comm_retval = cfd_proxy_comm_write_response(comm, retval, 2, 1, 0,
 
200
                                              int_out, double_out, NULL);
 
201
 
 
202
  if (n_vals > 0 && retval == 0 && comm_retval == 0)
 
203
    comm_retval = cfd_proxy_comm_write(comm, buffer, type_size, n_vals);
 
204
 
 
205
  if (n_vals_max > 0)
 
206
    CFDP_FREE(buffer);
 
207
 
 
208
  return comm_retval;
 
209
}
 
210
 
 
211
//============================================================================
 
212
// Public function definitions
 
213
//============================================================================
 
214
 
 
215
//----------------------------------------------------------------------------
 
216
// Forward a call from the client and its response.
 
217
//
 
218
// parameters:
 
219
//   comm   <-> communicator
 
220
//   r      <-> container for temporary communication request data
 
221
//
 
222
// returns:
 
223
//   0 if request was forwarded correctly, -1 for end-of-file, -2 for error
 
224
//----------------------------------------------------------------------------
 
225
 
 
226
int
 
227
cfd_proxy_forward(cfd_proxy_comm_t          *comm,
 
228
                  cfd_proxy_comm_request_t  *r)
 
229
{
 
230
  int comm_retval = 0;
 
231
 
 
232
  comm_retval = cfd_proxy_comm_read_request(comm, r);
 
233
 
 
234
  if (comm_retval != 0)
 
235
    return comm_retval;
 
236
 
 
237
  assert(   r->comp_id >= 0
 
238
         && r->comp_id < cfd_proxy_glob_n_components);
 
239
 
 
240
  // Handle function depending on function name
 
241
 
 
242
  if (strncmp(r->func_name, "cp_e", 4) == 0) {
 
243
 
 
244
    if (strncmp(r->func_name, "cp_een", 32) == 0)
 
245
      comm_retval = _forward_calcium_write(comm, r, CFD_PROXY_TYPE_int);
 
246
    else if (strncmp(r->func_name, "cp_ere", 32) == 0)
 
247
      comm_retval = _forward_calcium_write(comm, r, CFD_PROXY_TYPE_float);
 
248
    else if (strncmp(r->func_name, "cp_edb", 32) == 0)
 
249
      comm_retval = _forward_calcium_write(comm, r, CFD_PROXY_TYPE_double);
 
250
 
 
251
  }
 
252
  else if (strncmp(r->func_name, "cp_l", 4) == 0) {
 
253
 
 
254
    if (strncmp(r->func_name, "cp_len", 32) == 0)
 
255
      comm_retval = _forward_calcium_read(comm, r, CFD_PROXY_TYPE_int);
 
256
    else if (strncmp(r->func_name, "cp_lre", 32) == 0)
 
257
      comm_retval = _forward_calcium_read(comm, r, CFD_PROXY_TYPE_float);
 
258
    else if (strncmp(r->func_name, "cp_ldb", 32) == 0)
 
259
      comm_retval = _forward_calcium_read(comm, r, CFD_PROXY_TYPE_double);
 
260
 
 
261
  }
 
262
 
 
263
  else if (strncmp(r->func_name, "cp_cd", 32) == 0) {
 
264
 
 
265
    void *component = cfd_proxy_glob_component[r->comp_id];
 
266
    char instance_name[256]; // Normally, INSTANCE_LEN = 72
 
267
    const char *string_vals[] = {instance_name};
 
268
    int retval = 0;
 
269
 
 
270
    memset(instance_name, '\0', sizeof(instance_name));
 
271
 
 
272
    retval = cp_cd(component, instance_name);
 
273
 
 
274
    comm_retval = cfd_proxy_comm_write_response(comm, retval, 0, 0, 1,
 
275
                                                NULL, NULL, string_vals);
 
276
 
 
277
  }
 
278
 
 
279
  else if (strncmp(r->func_name, "cp_fin", 32) == 0) {
 
280
 
 
281
    void *component = cfd_proxy_glob_component[r->comp_id];
 
282
    int   cont = r->int_vals[0];
 
283
    int retval = 0;
 
284
 
 
285
    retval = cp_fin(component, cont);
 
286
 
 
287
    comm_retval = cfd_proxy_comm_write_response(comm, retval, 0, 0, 0,
 
288
                                                NULL, NULL, NULL);
 
289
 
 
290
  }
 
291
 
 
292
  return comm_retval;
 
293
}
 
294
 
 
295
//----------------------------------------------------------------------------
 
296
// Forward all calls from the client and their responses.
 
297
//----------------------------------------------------------------------------
 
298
 
 
299
void
 
300
cfd_proxy_forward_all(cfd_proxy_comm_t  *comm)
 
301
{
 
302
  cfd_proxy_comm_request_t r;
 
303
 
 
304
  int retval = 0;
 
305
 
 
306
  if (comm == NULL)
 
307
    return;
 
308
 
 
309
  cfd_proxy_comm_init_request(&r);
 
310
 
 
311
  while (retval == 0) {
 
312
 
 
313
    retval = cfd_proxy_forward(comm, &r);
 
314
 
 
315
  }
 
316
 
 
317
  cfd_proxy_comm_finalize_request(&r);
 
318
}
 
319
 
 
320
//----------------------------------------------------------------------------
 
321
 
 
322
#ifdef __cplusplus
 
323
}
 
324
#endif /* __cplusplus */
 
325