|
Last change
on this file since 11843 was 11843, checked in by Eric.Larour, 14 years ago |
|
Made matlab and python layers more independent in the ISSM code
|
|
File size:
1.5 KB
|
| Line | |
|---|
| 1 | /*\file PrintfFunction.c
|
|---|
| 2 | *\brief: this function is used by the _printf_ macro, to take into account the
|
|---|
| 3 | *fact we may be running on a cluster.
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | #include <stdarg.h>
|
|---|
| 7 | #include <stdio.h>
|
|---|
| 8 | #include "../shared/shared.h"
|
|---|
| 9 | #include "../include/include.h"
|
|---|
| 10 |
|
|---|
| 11 | #ifdef _HAVE_MATLAB_
|
|---|
| 12 | #include "mex.h"
|
|---|
| 13 | #endif
|
|---|
| 14 |
|
|---|
| 15 | int PrintfFunction(const char* format,...){
|
|---|
| 16 | /*http://linux.die.net/man/3/vsnprintf*/
|
|---|
| 17 |
|
|---|
| 18 | /*string to be printed: */
|
|---|
| 19 | char *buffer = NULL;
|
|---|
| 20 | int n,size = 100;
|
|---|
| 21 | int string_size;
|
|---|
| 22 | extern int my_rank;
|
|---|
| 23 | extern int num_procs;
|
|---|
| 24 |
|
|---|
| 25 | //variable list of arguments
|
|---|
| 26 | va_list args;
|
|---|
| 27 |
|
|---|
| 28 | while(true){
|
|---|
| 29 |
|
|---|
| 30 | /*allocate buffer for given string size*/
|
|---|
| 31 | buffer=(char*)xmalloc(size*sizeof(char));
|
|---|
| 32 |
|
|---|
| 33 | /* Try to print in the allocated space. */
|
|---|
| 34 | va_start(args, format);
|
|---|
| 35 | #ifndef WIN32
|
|---|
| 36 | n=vsnprintf(buffer,size,format,args);
|
|---|
| 37 | #else
|
|---|
| 38 | n=vsnprintf(buffer,size,format,args);
|
|---|
| 39 | #endif
|
|---|
| 40 | va_end(args);
|
|---|
| 41 |
|
|---|
| 42 | /* If that worked, return the string. */
|
|---|
| 43 | if(n>-1 && n<size) break;
|
|---|
| 44 |
|
|---|
| 45 | /* Else try again with more space. */
|
|---|
| 46 | if(n>-1) /* glibc 2.1 */
|
|---|
| 47 | size=n+1; /* precisely what is needed */
|
|---|
| 48 | else /* glibc 2.0 */
|
|---|
| 49 | size*=2; /* twice the old size */
|
|---|
| 50 |
|
|---|
| 51 | xfree((void**)&buffer);
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | /*Ok, if we are running in parallel, get node 0 to print*/
|
|---|
| 55 | #ifdef _HAVE_MATLAB_
|
|---|
| 56 | mexPrintf(buffer);
|
|---|
| 57 | #else
|
|---|
| 58 | if(my_rank==0)printf(buffer);
|
|---|
| 59 | #endif
|
|---|
| 60 |
|
|---|
| 61 | /*Clean up and return*/
|
|---|
| 62 | xfree((void**)&buffer);
|
|---|
| 63 | return 1;
|
|---|
| 64 | }
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.