buf_for_proc.c

References to this file elsewhere.
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        © 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 #ifndef MS_SUA
58 # include <stdio.h>
59 #endif
60 #include <stdlib.h>
61 #include "rsl_lite.h"
62 #ifndef STUBMPI
63 #  include "mpi.h"
64 #endif
65 
66 typedef struct bufdesc {
67   char * buf ;
68   int size ;
69 } bufdesc_t ;
70 
71 /* buftab[RSL_SENDBUF] is send buffer descriptor,
72    buftab[RSL_RECVBUF] is recv buffer descriptor. */
73 static bufdesc_t buftab[2][RSL_MAXPROC] ;
74 static int first = 1 ;
75 
76 /* 
77    buffer_for_proc
78 
79    returns a pointer to a buffer already allocated for processor P if
80    it is big enough; otherwise, it frees the existing buffer, if there
81    is one and then allocates a new one that is big enough.  If RSL_FREEBUF
82    is called for a P, the two buffers (send and recv) are truncated and
83    freed and NULL is returned.
84 
85    You are guaranteed to get back the same buffer as the previous call
86    for a given P, as long as the size is less than the size passed to
87    the previous call.  Thus, you can use this routine to manage the
88    pointers to the buffers for P and avoid having to set up arrays
89    of pointers in the routines that use these buffers.
90 
91 */
92 
93 char *
94 buffer_for_proc( P, size, code )
95   int P ;   /* processor number */
96   int size,		/* requested size */
97       code ;		/* RSL_SENDBUF, RSL_RECVBUF, or RSL_FREEBUF */
98 {
99   int p ;
100   int i, j ;
101   char mess[1024] ;
102   char * ret ;
103 
104   ret = NULL ;
105   if ( first )
106   {
107     for ( p = 0 ; p < RSL_MAXPROC ; p++ )
108     {
109       buftab[0][p].buf  = NULL ;    
110       buftab[1][p].buf  = NULL ;    
111       buftab[0][p].size = 0 ;    
112       buftab[1][p].size = 0 ;    
113     }
114     first = 0 ;
115   }
116   if ( P < 0 || P >= RSL_MAXPROC )
117   {
118     sprintf(mess,"Bad P argument to buffer_for_proc.  P = %d. Has RSL_MESH been called?\n",P) ;
119     RSL_TEST_ERR( 1, mess ) ;
120   }
121   if ( code == RSL_FREEBUF )
122   {
123 /* fprintf(stderr,"buffer_for_proc freeing buffer %d\n",P) ; */
124     if ( buftab[0][P].buf != NULL ) RSL_FREE( buftab[0][P].buf ) ;
125     if ( buftab[1][P].buf != NULL ) RSL_FREE( buftab[1][P].buf ) ;
126     buftab[0][P].buf  = NULL ;    
127     buftab[1][P].buf  = NULL ;    
128     buftab[0][P].size = 0 ;    
129     buftab[1][P].size = 0 ;    
130 /* show_tot_size() ; */
131   }
132   else if ( code == RSL_SENDBUF || code == RSL_RECVBUF )
133   {
134     if ( buftab[code][P].size < size )
135     {
136 #if 0
137 fprintf(stderr,"buffer_for_proc %s %d : was %d, increasing to %d\n",
138          (code == RSL_SENDBUF)?"RSL_SENDBUF":"RSL_RECVBUF",
139 	 P,buftab[code][P].size, size+512) ;
140 #endif
141       if ( buftab[code][P].buf != NULL ) RSL_FREE( buftab[code][P].buf ) ;
142       buftab[code][P].buf = RSL_MALLOC(char,size+512) ;
143       buftab[code][P].size = size+512 ;
144 /* show_tot_size() ; */
145     }
146     ret = buftab[code][P].buf ;
147   }
148   return(ret) ;
149 }
150 
151 show_tot_size()
152 {
153   int P ;
154   int acc ;
155   acc = 0 ;
156   for ( P = 0 ; P < RSL_MAXPROC ; P++ )
157   {
158     acc += buftab[0][P].size ;
159     acc += buftab[1][P].size ;
160   }
161 #ifndef MS_SUA
162   fprintf(stderr,"Total bytes allocated for buffers: %d\n", acc ) ;
163 #endif
164 }
165 
166 int
167 buffer_size_for_proc( P, code )
168   int P ;
169   int code ;
170 {
171   return( buftab[code][P].size ) ;
172 }