libshmem/demo/smdemo0_rcv.c

206 lines
5.9 KiB
C
Raw Normal View History

/*----------------------------------------------------------------------------*/
/* smdemo0_rcv.c */
/*----------------------------------------------------------------------------*/
2005-06-27 01:16:38 +02:00
/*----------------------------------------------------------------------------*/
/* This file is part of LibShMem */
/* */
/* LibShMem is free software: you can redistribute it and/or modify it */
/* under the terms of the GNU Lesser General Public License as published */
/* by the Free Software Foundation, either version 3 of the License, or */
/* (at your option) any later version. */
/* */
/* LibShMem is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU Lesser General Public License for more details. */
/* */
/* You should have received a copy of the GNU Lesser General Public */
/* License along with Drummer. If not, see */
/* <https://www.gnu.org/licenses/>. */
/*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
/* Includes */
/*----------------------------------------------------------------------------*/
2005-06-27 01:16:38 +02:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <unistd.h>
#include <errno.h>
#include <node.h>
#ifndef SM_MODE
# define SM_MODE 0 /* Utilisation des API s<>curis<69>s */
#endif
#include <shmem.h>
#define LOG_ERROR 10
#define LOG_WARNING 20
#define LOG_INFO 30
#define LOG_TRACE 40
void Log_Print( int, int, int, char *, ...);
int main( int, char **);
void Log_Print( int Log_Level, int Verbose_Level, int Inst_Id, char *Format_Str, ...)
{
va_list args;
char *prompt = "rcv";
int fmt_size = 255;
char fmt[fmt_size + 1];
if( Log_Level <= Verbose_Level)
{
va_start( args, Format_Str);
snprintf( fmt, fmt_size, "%s%02d : %s", prompt, Inst_Id, Format_Str);
vfprintf( stderr, fmt, args);
fflush( stderr);
va_end( args);
}
}
int main( int ArgC, char **ArgV)
{
int Inst_Id;
char *Heap_Name;
int Verbose_Level = 30;
SMT_Heap *heap_ptr;
int locked;
SMT_Status status = SMS_OK;
char *chunk_ptr;
int chunk_id = 0;
char input_str[100];
size_t input_size = 99;
if( ArgC != 4)
{
Log_Print( LOG_ERROR, Verbose_Level, 0, "Usage: %s Inst_Id Heap_Name Verbose_Level\n", ArgV[0]);
exit( -1);
}
else
{
Inst_Id = atoi( ArgV[1]);
Heap_Name = ArgV[2];
Verbose_Level = atoi( ArgV[3]);
Log_Print( LOG_INFO, Verbose_Level, Inst_Id, "Inst_Id: (%d) Heap_Name: [%s] Verbose_Level: (%d)...\n",
Inst_Id, Heap_Name, Verbose_Level);
fflush( stderr);
}
if( ( status = SM_Library_Open( 0, NULL, ( SMD_OPEN | SMD_DEBUG_ALL))) != SMS_OK)
{
Log_Print( LOG_ERROR, Verbose_Level, Inst_Id, "Can't open LibShMem (%d) !\n", status);
exit( -1);
}
Log_Print( LOG_TRACE, Verbose_Level, Inst_Id, "LibShMem opened !\n");
if( ( status = SM_Heap_Open( Heap_Name, &heap_ptr, 0, ( SMD_OPEN | SMD_NO_LOCK), &locked)) != SMS_OK)
{
Log_Print( LOG_ERROR, Verbose_Level, Inst_Id, "Can't open heap (%d) !\n", status);
}
else
{
Log_Print( LOG_TRACE, Verbose_Level, Inst_Id, "Heap opened !\n");
while( ( fgets( input_str, input_size, stdin) != NULL) && ( status == SMS_OK))
{
if( ( chunk_ptr = (char *)atol( input_str)) == NULL)
{
Log_Print( LOG_ERROR, Verbose_Level, Inst_Id, "Garbage on stdin [%s], skiping !\n", input_str);
}
else
{
Log_Print( LOG_TRACE, Verbose_Level, Inst_Id, "Received message: [%s] Id: (%d) Address: (%lu)\n", chunk_ptr, chunk_id, chunk_ptr);
chunk_id++;
if( ( ( status = SM_Heap_Lock( heap_ptr, SMD_WRITE, &locked)) != SMS_OK) && ( locked == TRUE))
{
Log_Print( LOG_ERROR, Verbose_Level, Inst_Id, "Can't lock heap (%d) !\n", status);
}
else
{
if( ( status = SM_Chunk_Free( heap_ptr, chunk_ptr)) != SMS_OK)
{
Log_Print( LOG_ERROR, Verbose_Level, Inst_Id, "Can't free chunk (%d) !\n", status);
}
else
{
Log_Print( LOG_TRACE, Verbose_Level, Inst_Id, "Chunk: (%lu) freed !\n", chunk_ptr);
if( ( status = SM_Heap_Unlock( heap_ptr)) != SMS_OK)
{
Log_Print( LOG_ERROR, Verbose_Level, Inst_Id, "Can't unlock heap (%d) !\n", status);
}
}
}
}
}
Log_Print( LOG_INFO, Verbose_Level, Inst_Id, "Number of message received: (%d) !\n", chunk_id);
if( ( ( status = SM_Heap_Lock( heap_ptr, SMD_WRITE, &locked)) != SMS_OK) && ( locked == TRUE))
{
Log_Print( LOG_ERROR, Verbose_Level, Inst_Id, "Can't lock heap (%d) !\n", status);
}
else
{
if( ( status = SM_Heap_Close( heap_ptr)) != SMS_OK)
{
Log_Print( LOG_ERROR, Verbose_Level, Inst_Id, "Can't close heap (%d) !\n", status);
}
else
{
Log_Print( LOG_TRACE, Verbose_Level, Inst_Id, "Heap closed !\n");
}
}
}
if( ( status = SM_Library_Close( SMD_CLOSE)) != SMS_OK)
{
Log_Print( LOG_ERROR, Verbose_Level, Inst_Id, "Can't close LibShMem (%d) !\n", status);
exit( -1);
}
else
{
Log_Print( LOG_TRACE, Verbose_Level, Inst_Id, "LibShMem closed !\n");
}
exit( 0);
}