source: trunk/WRF.COMMON/WRFV2/external/RSL_LITE/buf_for_proc.c @ 3552

Last change on this file since 3552 was 11, checked in by aslmd, 14 years ago

spiga@svn-planeto:ajoute le modele meso-echelle martien

  • Property svn:executable set to *
File size: 5.5 KB
Line 
1/***********************************************************************
2     
3                              COPYRIGHT
4     
5     The following is a notice of limited availability of the code and
6     Government license and disclaimer which must be included in the
7     prologue of the code and in all source listings of the code.
8     
9     Copyright notice
10       (c) 1977  University of Chicago
11     
12     Permission is hereby granted to use, reproduce, prepare
13     derivative works, and to redistribute to others at no charge.  If
14     you distribute a copy or copies of the Software, or you modify a
15     copy or copies of the Software or any portion of it, thus forming
16     a work based on the Software and make and/or distribute copies of
17     such work, you must meet the following conditions:
18     
19          a) If you make a copy of the Software (modified or verbatim)
20             it must include the copyright notice and Government       
21             license and disclaimer.
22     
23          b) You must cause the modified Software to carry prominent   
24             notices stating that you changed specified portions of   
25             the Software.
26     
27     This software was authored by:
28     
29     Argonne National Laboratory
30     J. Michalakes: (630) 252-6646; email: michalak@mcs.anl.gov
31     Mathematics and Computer Science Division
32     Argonne National Laboratory, Argonne, IL  60439
33     
34     ARGONNE NATIONAL LABORATORY (ANL), WITH FACILITIES IN THE STATES
35     OF ILLINOIS AND IDAHO, IS OWNED BY THE UNITED STATES GOVERNMENT,
36     AND OPERATED BY THE UNIVERSITY OF CHICAGO UNDER PROVISION OF A
37     CONTRACT WITH THE DEPARTMENT OF ENERGY.
38     
39                      GOVERNMENT LICENSE AND DISCLAIMER
40     
41     This computer code material was prepared, in part, as an account
42     of work sponsored by an agency of the United States Government.
43     The Government is granted for itself and others acting on its
44     behalf a paid-up, nonexclusive, irrevocable worldwide license in
45     this data to reproduce, prepare derivative works, distribute
46     copies to the public, perform publicly and display publicly, and
47     to permit others to do so.  NEITHER THE UNITED STATES GOVERNMENT
48     NOR ANY AGENCY THEREOF, NOR THE UNIVERSITY OF CHICAGO, NOR ANY OF
49     THEIR EMPLOYEES, MAKES ANY WARRANTY, EXPRESS OR IMPLIED, OR
50     ASSUMES ANY LEGAL LIABILITY OR RESPONSIBILITY FOR THE ACCURACY,
51     COMPLETENESS, OR USEFULNESS OF ANY INFORMATION, APPARATUS,
52     PRODUCT, OR PROCESS DISCLOSED, OR REPRESENTS THAT ITS USE WOULD
53     NOT INFRINGE PRIVATELY OWNED RIGHTS.
54
55***************************************************************************/
56
57#include <stdio.h>
58#include <stdlib.h>
59#include "rsl_lite.h"
60#include "mpi.h"
61
62typedef struct bufdesc {
63  char * buf ;
64  int size ;
65} bufdesc_t ;
66
67/* buftab[RSL_SENDBUF] is send buffer descriptor,
68   buftab[RSL_RECVBUF] is recv buffer descriptor. */
69static bufdesc_t buftab[2][RSL_MAXPROC] ;
70static int first = 1 ;
71
72/*
73   buffer_for_proc
74
75   returns a pointer to a buffer already allocated for processor P if
76   it is big enough; otherwise, it frees the existing buffer, if there
77   is one and then allocates a new one that is big enough.  If RSL_FREEBUF
78   is called for a P, the two buffers (send and recv) are truncated and
79   freed and NULL is returned.
80
81   You are guaranteed to get back the same buffer as the previous call
82   for a given P, as long as the size is less than the size passed to
83   the previous call.  Thus, you can use this routine to manage the
84   pointers to the buffers for P and avoid having to set up arrays
85   of pointers in the routines that use these buffers.
86
87*/
88
89char mess[1024] ;
90
91char *
92buffer_for_proc( P, size, code )
93  int P ;   /* processor number */
94  int size,             /* requested size */
95      code ;            /* RSL_SENDBUF, RSL_RECVBUF, or RSL_FREEBUF */
96{
97  int p ;
98  int i, j ;
99  char * ret ;
100
101  ret = NULL ;
102  if ( first )
103  {
104    for ( p = 0 ; p < RSL_MAXPROC ; p++ )
105    {
106      buftab[0][p].buf  = NULL ;   
107      buftab[1][p].buf  = NULL ;   
108      buftab[0][p].size = 0 ;   
109      buftab[1][p].size = 0 ;   
110    }
111    first = 0 ;
112  }
113  if ( P < 0 || P >= RSL_MAXPROC )
114  {
115    sprintf(mess,"Bad P argument to buffer_for_proc.  P = %d. Has RSL_MESH been called?\n",P) ;
116    RSL_TEST_ERR( 1, mess ) ;
117  }
118  if ( code == RSL_FREEBUF )
119  {
120/* fprintf(stderr,"buffer_for_proc freeing buffer %d\n",P) ; */
121    if ( buftab[0][P].buf != NULL ) RSL_FREE( buftab[0][P].buf ) ;
122    if ( buftab[1][P].buf != NULL ) RSL_FREE( buftab[1][P].buf ) ;
123    buftab[0][P].buf  = NULL ;   
124    buftab[1][P].buf  = NULL ;   
125    buftab[0][P].size = 0 ;   
126    buftab[1][P].size = 0 ;   
127/* show_tot_size() ; */
128  }
129  else if ( code == RSL_SENDBUF || code == RSL_RECVBUF )
130  {
131    if ( buftab[code][P].size < size )
132    {
133#if 0
134fprintf(stderr,"buffer_for_proc %s %d : was %d, increasing to %d\n",
135         (code == RSL_SENDBUF)?"RSL_SENDBUF":"RSL_RECVBUF",
136         P,buftab[code][P].size, size+512) ;
137#endif
138      if ( buftab[code][P].buf != NULL ) RSL_FREE( buftab[code][P].buf ) ;
139      buftab[code][P].buf = RSL_MALLOC(char,size+512) ;
140      buftab[code][P].size = size+512 ;
141/* show_tot_size() ; */
142    }
143    ret = buftab[code][P].buf ;
144  }
145  return(ret) ;
146}
147
148show_tot_size()
149{
150  int P ;
151  int acc ;
152  acc = 0 ;
153  for ( P = 0 ; P < RSL_MAXPROC ; P++ )
154  {
155    acc += buftab[0][P].size ;
156    acc += buftab[1][P].size ;
157  }
158  fprintf(stderr,"Total bytes allocated for buffers: %d\n", acc ) ;
159}
160
161int
162buffer_size_for_proc( P, code )
163  int P ;
164  int code ;
165{
166  return( buftab[code][P].size ) ;
167}
Note: See TracBrowser for help on using the repository browser.