| 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 | int PrintfFunction(const char* format,...){
|
|---|
| 12 | /*http://linux.die.net/man/3/vsnprintf*/
|
|---|
| 13 |
|
|---|
| 14 | /*string to be printed: */
|
|---|
| 15 | char *buffer = NULL;
|
|---|
| 16 | int n,size = 100;
|
|---|
| 17 | int my_rank;
|
|---|
| 18 | //variable list of arguments
|
|---|
| 19 | va_list args;
|
|---|
| 20 |
|
|---|
| 21 | /*recover my_rank:*/
|
|---|
| 22 | my_rank=IssmComm::GetRank();
|
|---|
| 23 |
|
|---|
| 24 | while(true){
|
|---|
| 25 |
|
|---|
| 26 | /*allocate buffer for given string size*/
|
|---|
| 27 | buffer=xNew<char>(size);
|
|---|
| 28 |
|
|---|
| 29 | /* Try to print in the allocated space. */
|
|---|
| 30 | va_start(args, format);
|
|---|
| 31 | n=vsnprintf(buffer,size,format,args);
|
|---|
| 32 | va_end(args);
|
|---|
| 33 |
|
|---|
| 34 | /* If that worked, return the string. */
|
|---|
| 35 | if(n>-1 && n<size) break;
|
|---|
| 36 |
|
|---|
| 37 | /* Else try again with more space. */
|
|---|
| 38 | if(n>-1) /* glibc 2.1 */
|
|---|
| 39 | size=n+1; /* precisely what is needed */
|
|---|
| 40 | else /* glibc 2.0 */
|
|---|
| 41 | size*=2; /* twice the old size */
|
|---|
| 42 |
|
|---|
| 43 | xDelete<char>(buffer);
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | /*Ok, if we are running in parallel, get node 0 to print*/
|
|---|
| 47 | if(my_rank==0)_printString_(buffer);
|
|---|
| 48 |
|
|---|
| 49 | /*Clean up and return*/
|
|---|
| 50 | xDelete<char>(buffer);
|
|---|
| 51 | return 1;
|
|---|
| 52 | }
|
|---|
| 53 | int PrintfFunction(const string & message){
|
|---|
| 54 | int my_rank;
|
|---|
| 55 |
|
|---|
| 56 | /*recover my_rank:*/
|
|---|
| 57 | my_rank=IssmComm::GetRank();
|
|---|
| 58 |
|
|---|
| 59 | if(my_rank==0){
|
|---|
| 60 | printf("%s\n",message.c_str());
|
|---|
| 61 | }
|
|---|
| 62 | return 1;
|
|---|
| 63 | }
|
|---|
| 64 | int PrintfFunction2(const string & message){
|
|---|
| 65 | int my_rank;
|
|---|
| 66 |
|
|---|
| 67 | /*recover my_rank:*/
|
|---|
| 68 | my_rank=IssmComm::GetRank();
|
|---|
| 69 |
|
|---|
| 70 | if(my_rank==0){
|
|---|
| 71 | printf("%s",message.c_str());
|
|---|
| 72 | }
|
|---|
| 73 | return 1;
|
|---|
| 74 | }
|
|---|