sgeenviron.cΒΆ

/*
 *  Copyright 2010. The Regents of the University of California.
 *  All Rights Reserved.  
 *  @author  Prakashan Korambath
 *
 *  Purpose:  Make use of scheduler TMPDIR variable for File I/O.  The scratch space
 *  defined in SGE is often has a faster I/O than NFS file system.  So your codes
 *  may run faster if you use TMPDIR to write scratch files which are not needed
 *  at the end of a run.  The scheduler used on Hoffman2 is Oracle Grid Engine.
 *  In place of TMPDIR any other faster SCRATCH space can be substituted.
 *
 *  This software program and documentation are copyrighted by The Regents
 *  of the University of California. The software program and documentation
 *  are supplied "as is", without any accompanying services from The Regents.
 *  The Regents does not warrant that the operation of the program will be
 *  uninterrupted or error-free. The end-user understands that the program
 *  was developed for research purposes and is advised not to rely exclusively
 *  on the program for any reason.

 *  IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
 *  DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING
 *  LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
 *  EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY
 *  OF SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY
 *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED
 *  HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO
 *  OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR
 *  MODIFICATIONS.
 *  
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define FILE_PATH_CHAR 400

int main(int argc,char *argv[]) {

  int index,i;
  // First get the SGE environment variable TMPDIR

  const char *tmpdir = getenv ("TMPDIR");

  if (0 == tmpdir || '\0' == *tmpdir) {
       printf("Failed to get environment variable TMPDIR \n");
       // set it to /work for testing in a non SGE environment
       tmpdir = "/work";
  }

  printf ("TMPDIR is %s \n", tmpdir);

  //  The name of the I/O file in the scratch directory (remember to add / in front)

  static const char scratch1[] = "/scratchxxx";

  // declare buffer memory of scratchfile path and file name

  char filepath [FILE_PATH_CHAR];


  // check to see if the buffer is large enough

  const size_t len = strlen (tmpdir);

  if (sizeof filepath < (len + sizeof scratch1 )) {
     printf ("File Path buffer is too small \n");
     exit(1);
  }

  // construct a temporary pathname for the scratch file

  memcpy (filepath, tmpdir, len);

  //  Copy the actual file name as well

  memcpy (filepath+len, scratch1, sizeof scratch1);

  
  printf ("Full path is %s \n", filepath);

  FILE *writefile;

  if (filepath) {
      // open the scratch file to write
      writefile = fopen (filepath, "w");
  }
  else {
    printf("Scratch File path not defined \n");
  }
  
  // write something to the file

  for (index =0; index < 10; index++) {
      fwrite( &index, sizeof(int), 1, writefile);
  }

  fclose(writefile);

  FILE *readfile;

  if (filepath) {
      // open the scratch file to read
      readfile = fopen (filepath, "r");
  }
  else {
    printf("Scratch File path not defined \n");

  }

  // Just read in to memory of a variable readint and print it out
  // If you need to stor it declare an array

  int readint;
  for (i =0; i < 10; i++) {
      fread( &readint, sizeof(int), 1, readfile);
      printf("Read back values are %d \n", readint);
  }

  fclose(readfile);

  // Finally remove the file 

  remove (filepath);
}