/* ######################################################################## */
/* File:    Lib.c                                                           */
/* Author:  Yang, Li                                                        */
/* (C)      This file is some common subroutines I use frequently.          */
/*                                                                          */
/* Date:    Dec, 10, 2000                                                   */
/* ######################################################################## */

#include <stdio.h>
#include <math.h>
#include <sys/time.h>
#include <sys/resource.h>

#define MAXClockNum        5

struct rusage              ru[MAXClockNum];
struct timeval             start_stime[MAXClockNum];
struct timeval             start_utime[MAXClockNum];
struct timeval             end_stime[MAXClockNum];
struct timeval             end_utime[MAXClockNum];

typedef struct my_time{
    double stime;
    double utime;
}myTime;

/* ######################################################################## */
/*          The file contains:                                              */
/* (C)      openFile( )                                                     */
/* (C)	    power( )							    */
/* ######################################################################## */


/* ######################################################################## */
/* FILE * openFile(char *fName, char *attr)                                 */
/*                                                                          */
/* (C)  fName specifies the path and name of the file you are going         */
/* (C)  to open;                                                            */
/* (C)  attr specifies the opening mode, which could be:                    */
/* (C)  r or rb            Open file for reading.                           */  
/* (C)  w or wb            Truncate to  zero  length  or  create            */
/* (C)                     file for writing.                                */
/* (C)  a or ab            Append; open or create file for writ-            */
/* (C)                     ing at end-of-file.                              */
/* (C)  r+ or rb+ or r+b   Open file  for  update  (reading  and            */
/* (C)                     writing).                                        */
/* (C)  w+ or wb+ or w+b   Truncate to  zero  length  or  create            */
/* (C)                     file for update.                                 */
/* (C)  a+ or ab+ or a+b   Append;  open  or  create  file   for            */
/* (C)                     update, writing at end-of-file.                  */
/*                                                                          */
/* ######################################################################## */

FILE * openFile(char *fName,char *attr)
{
  FILE * fp;

  if((fp = fopen(fName, attr)) == NULL){
     printf("\n %s not found! \n", fName);
     exit(0);    
  }
  else {
      printf("\n %s opened successfully. ", fName);
      return(fp);
  }
}

double power(double a, long n){

  long i;
  double x;

  x = 1.0;
  for (i = 0; i < n; i++)
     x = x * a;

  return(x);
}

/**********************************************
* Routines: void BeginTiming(int i)
*           double EndTiming(int i)
* Usage:    BeginTiming(0);
*           <your procedure here>
*           runtime = EndTiming(0);
***********************************************/

void BeginTiming(int i)
{
  i = i % MAXClockNum;
  getrusage(RUSAGE_SELF,&ru[i]);
  start_stime[i] = ru[i].ru_stime;
  start_utime[i] = ru[i].ru_utime;

  /*printf("\n Start time is %ld(s), %ld(us)", start_time[i].tv_sec, start_time[i].tv_usec);*/
}

myTime EndTiming(int i)
{
  double t;
  myTime _mytime;

  i = i % MAXClockNum;
  getrusage(RUSAGE_SELF,&ru[i]);
  end_stime[i] = ru[i].ru_stime;
  end_utime[i] = ru[i].ru_utime;
  if (end_stime[i].tv_usec < start_stime[i].tv_usec)
        {
      end_stime[i].tv_usec += 1000000;
      end_stime[i].tv_sec -= 1;
    }
  t = (end_stime[i].tv_sec - start_stime[i].tv_sec)*1000000
      + (end_stime[i].tv_usec - start_stime[i].tv_usec);
  t=t/1000000.0;
  _mytime.stime = t;


  if (end_utime[i].tv_usec < start_utime[i].tv_usec)
        {
      end_utime[i].tv_usec += 1000000;
      end_utime[i].tv_sec -= 1;
    }
  t = (end_utime[i].tv_sec - start_utime[i].tv_sec)*1000000
      + (end_utime[i].tv_usec - start_utime[i].tv_usec);
  t=t/1000000.0;
  _mytime.utime = t;


  /*printf("\nt = %lf", t);
  printf("\n End time is %lds, %ldus", end_time[i].tv_sec, end_time[i].tv_usec); */
  return (_mytime);
}

/* ################################################################################## */
/* long MemoryStatics(int who)							      */
/* Usage: Given the process, return the memory allocated to this process.	      */
/*										      */
/* ################################################################################## */

long CountMemory(int who){

	who = who % MAXClockNum;

	getrusage(RUSAGE_SELF, &ru[who]);

	return ru[who].ru_ixrss + ru[who].ru_idrss + ru[who].ru_isrss;
}

