/***********************************************************************
     
                              COPYRIGHT
     
     The following is a notice of limited availability of the code and 
     Government license and disclaimer which must be included in the 
     prologue of the code and in all source listings of the code.
     
     Copyright notice
       (c) 1977  University of Chicago
     
     Permission is hereby granted to use, reproduce, prepare 
     derivative works, and to redistribute to others at no charge.  If 
     you distribute a copy or copies of the Software, or you modify a 
     copy or copies of the Software or any portion of it, thus forming 
     a work based on the Software and make and/or distribute copies of 
     such work, you must meet the following conditions:
     
          a) If you make a copy of the Software (modified or verbatim) 
             it must include the copyright notice and Government       
             license and disclaimer.
     
          b) You must cause the modified Software to carry prominent   
             notices stating that you changed specified portions of    
             the Software.
     
     This software was authored by:
     
     Argonne National Laboratory
     J. Michalakes: (630) 252-6646; email: michalak@mcs.anl.gov
     Mathematics and Computer Science Division
     Argonne National Laboratory, Argonne, IL  60439
     
     ARGONNE NATIONAL LABORATORY (ANL), WITH FACILITIES IN THE STATES 
     OF ILLINOIS AND IDAHO, IS OWNED BY THE UNITED STATES GOVERNMENT, 
     AND OPERATED BY THE UNIVERSITY OF CHICAGO UNDER PROVISION OF A 
     CONTRACT WITH THE DEPARTMENT OF ENERGY.
     
                      GOVERNMENT LICENSE AND DISCLAIMER
     
     This computer code material was prepared, in part, as an account 
     of work sponsored by an agency of the United States Government.
     The Government is granted for itself and others acting on its 
     behalf a paid-up, nonexclusive, irrevocable worldwide license in 
     this data to reproduce, prepare derivative works, distribute 
     copies to the public, perform publicly and display publicly, and 
     to permit others to do so.  NEITHER THE UNITED STATES GOVERNMENT 
     NOR ANY AGENCY THEREOF, NOR THE UNIVERSITY OF CHICAGO, NOR ANY OF 
     THEIR EMPLOYEES, MAKES ANY WARRANTY, EXPRESS OR IMPLIED, OR 
     ASSUMES ANY LEGAL LIABILITY OR RESPONSIBILITY FOR THE ACCURACY, 
     COMPLETENESS, OR USEFULNESS OF ANY INFORMATION, APPARATUS, 
     PRODUCT, OR PROCESS DISCLOSED, OR REPRESENTS THAT ITS USE WOULD 
     NOT INFRINGE PRIVATELY OWNED RIGHTS.

***************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include "rsl.h"

typedef struct bufdesc {
  char * buf ;
  int size ;
} bufdesc_t ;

/* buftab[RSL_SENDBUF] is send buffer descriptor,
   buftab[RSL_RECVBUF] is recv buffer descriptor. */
static bufdesc_t buftab[2][RSL_MAXPROC] ;
static int first = 1 ;

#ifdef NEC_TUNE
/*
   NECNOTE:
   modify RSL message buffering scheme to utilize NEC Global Memory
   to aid in performance of MPI Message Passing on Multi-node SX machines.
*/
#include <mpi.h>
#include <malloc.h>
#define MAXBUFSIZE 1024*1024*64 /* NEC Global Memory buffer size */
#endif
/* 
   buffer_for_proc

   returns a pointer to a buffer already allocated for processor P if
   it is big enough; otherwise, it frees the existing buffer, if there
   is one and then allocates a new one that is big enough.  If RSL_FREEBUF
   is called for a P, the two buffers (send and recv) are truncated and
   freed and NULL is returned.

   You are guaranteed to get back the same buffer as the previous call
   for a given P, as long as the size is less than the size passed to
   the previous call.  Thus, you can use this routine to manage the
   pointers to the buffers for P and avoid having to set up arrays
   of pointers in the routines that use these buffers.

*/

char *
buffer_for_proc( P, size, code )
  rsl_processor_t P ;   /* processor number */
  int size,		/* requested size */
      code ;		/* RSL_SENDBUF, RSL_RECVBUF, or RSL_FREEBUF */
{
  rsl_processor_t p ;
  int i, j ;
  char * ret ;
#ifdef NEC_TUNE
  static char * private_buf ; /* pointer to NEC Global Memory */
  static int cur_indx ;       /* size of total user memory requests */
#endif

  ret = NULL ;
  if ( first )
  {
    for ( p = 0 ; p < RSL_MAXPROC ; p++ )
    {
      buftab[0][p].buf  = NULL ;    
      buftab[1][p].buf  = NULL ;    
      buftab[0][p].size = 0 ;    
      buftab[1][p].size = 0 ;    
    }
#ifdef NEC_TUNE
    MPI_Alloc_mem(MAXBUFSIZE, MPI_INFO_NULL, &private_buf) ;
    cur_indx = 0 ;
#endif
    first = 0 ;
  }
  if ( P < 0 || P >= RSL_MAXPROC )
  {
#ifndef NEC_TUNE
    sprintf(mess,"Bad P argument to buffer_for_proc.  P = %d. Has RSL_MESH been called?\n",P) ;
    RSL_TEST_ERR( 1, mess ) ;
#else
    fprintf(stderr,"Bad P argument to buffer_for_proc.  P = %d. Has RSL_MESH been called?\n",P) ;
#endif
  }
  if ( code == RSL_FREEBUF )
  {
/* fprintf(stderr,"buffer_for_proc freeing buffer %d\n",P) ; */
/* NECNOTE: do not free Global Memory */
#ifndef NEC_TUNE
    if ( buftab[0][P].buf != NULL ) RSL_FREE( buftab[0][P].buf ) ;
    if ( buftab[1][P].buf != NULL ) RSL_FREE( buftab[1][P].buf ) ;
    buftab[0][P].buf  = NULL ;    
    buftab[1][P].buf  = NULL ;    
    buftab[0][P].size = 0 ;    
    buftab[1][P].size = 0 ;    
#endif
/* show_tot_size() ; */
  }
  else if ( code == RSL_SENDBUF || code == RSL_RECVBUF )
  {
    if ( buftab[code][P].size < size )
    {
/* fprintf(stderr,"buffer_for_proc %s %d : was %d, increasing to %d\n",
         (code == RSL_SENDBUF)?"RSL_SENDBUF":"RSL_RECVBUF",
	 P,buftab[code][P].size, size+512) ; */
#ifndef NEC_TUNE
      if ( buftab[code][P].buf != NULL ) RSL_FREE( buftab[code][P].buf ) ;
      buftab[code][P].buf = RSL_MALLOC(char,size+512) ;
      buftab[code][P].size = size ;
#else
/*
   NECNOTE:
   if we exceed the GLOBAL MEMORY allocated, stop program.
   user should increase MAXBUFSIZE above to fix problem.
*/
      if( (cur_indx+size) > MAXBUFSIZE )
      {
        fprintf(stderr,
          "<<<buf_for_proc>>> exceeded MAXBUFSIZE %ld requested %ld bytes",
          MAXBUFSIZE, cur_indx+size) ;
        exit(3) ;
      }
      buftab[code][P].buf = private_buf ;
      buftab[code][P].size = size ;
      private_buf =  private_buf + size+512 ;
      cur_indx = cur_indx + size+512 ;
#endif
/* show_tot_size() ; */
    }
    ret = buftab[code][P].buf ;
  }
  return(ret) ;
}

show_tot_size()
{
  int P ;
  int acc ;
  acc = 0 ;
  for ( P = 0 ; P < RSL_MAXPROC ; P++ )
  {
    acc += buftab[0][P].size ;
    acc += buftab[1][P].size ;
  }
#ifndef NEC_TUNE
  fprintf(stderr,"Total bytes allocated for buffers: %d\n", acc ) ;
#else
  printf("Total bytes allocated for buffers: %d\n", acc ) ;
#endif
}

int
buffer_size_for_proc( P, code )
  int P ;
  int code ;
{
  return( buftab[code][P].size ) ;
}
