libshmem/demo/smdemo0_snd.c

205 lines
5.9 KiB
C
Raw Normal View History

/*----------------------------------------------------------------------------*/
/* smdemo0_snd.c */
/*----------------------------------------------------------------------------*/
2005-06-27 01:16:38 +02:00
/*----------------------------------------------------------------------------*/
2024-04-20 10:11:27 +02:00
/* This file is part of LibShMem. */
/* */
/* LibShMem is free software: you can redistribute it and/or modify it */
2024-04-20 10:11:27 +02:00
/* under the terms of the GNU 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 */
2024-04-20 10:11:27 +02:00
/* GNU General Public License for more details. */
/* */
2024-04-20 10:11:27 +02:00
/* You should have received a copy of the GNU General Public License */
/* along with LibShMem. 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 ArgC, char **ArgV);
void Log_Print( int Log_Level, int Verbose_Level, int Inst_Id, char *Format_Str, ...)
{
va_list args;
char *prompt = "snd";
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 Chunk_Nb;
size_t Chunk_Size;
int Verbose_Level = 30;
SMT_Heap *heap_ptr;
size_t heap_size = 1024;
int locked;
SMT_Status status;
char *chunk_ptr;
int chunk_id;
if( ArgC != 6)
{
Log_Print( LOG_ERROR, Verbose_Level, 0, "Usage: %s Inst_Id Heap_Name Chunck_Nb Chunk_Size Verbose_Level\n", ArgV[0]);
exit( -1);
}
else
{
Inst_Id = atoi( ArgV[1]);
Heap_Name = ArgV[2];
Chunk_Nb = atoi( ArgV[3]);
Chunk_Size = atoi( ArgV[4]);
Verbose_Level = atoi( ArgV[5]);
Log_Print( LOG_INFO, Verbose_Level, Inst_Id, "Inst_Id: (%d) Heap_Name: [%s] Chunck_Nb: (%d) Chunck_Size: (%d) Verbose_Level: (%d)...\n",
Inst_Id, Heap_Name, Chunk_Nb, Chunk_Size, Verbose_Level);
}
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, heap_size, ( 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");
for( chunk_id = 0; ( chunk_id < Chunk_Nb) && ( status == SMS_OK); 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_Alloc( heap_ptr, Chunk_Size, (void **)&chunk_ptr)) != SMS_OK)
{
Log_Print( LOG_ERROR, Verbose_Level, Inst_Id, "Can't alloc chunk (%d) !\n", status);
}
else
{
if( ( status = SM_Heap_Unlock( heap_ptr)) != SMS_OK)
{
Log_Print( LOG_ERROR, Verbose_Level, Inst_Id, "Can't unlock heap (%d) !\n", status);
}
else
{
Log_Print( LOG_TRACE, Verbose_Level, Inst_Id, "Chunk allocated !\n");
snprintf( chunk_ptr, Chunk_Size, "Hello brother: (%d) !", chunk_id);
Log_Print( LOG_TRACE, Verbose_Level, Inst_Id, "Message: [%s] writen into the chunk: (%lu)\n", chunk_ptr, chunk_ptr);
fprintf( stdout, "%lu\n", chunk_ptr);
fflush( stdout);
}
}
}
}
Log_Print( LOG_INFO, Verbose_Level, Inst_Id, "Number of message sendeded: (%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);
}