File: diskcall.c

package info (click to toggle)
aces3 3.0.6-7
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 82,460 kB
  • sloc: fortran: 225,647; ansic: 20,413; cpp: 4,349; makefile: 953; sh: 137
file content (345 lines) | stat: -rw-r--r-- 9,718 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*
*  Copyright (c) 2003-2010 University of Florida
*
*  This program is free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  This program 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
*  GNU General Public License for more details.
*
*  The GNU General Public License is included in this distribution
*  in the file COPYRIGHT.
*/ 
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <mpi.h>
#include "f77_name.h"
#include "f_types.h"

#ifdef BLUEGENE
#define _LARGEFILE64_SOURCE
#define _FILE_OFFSET_BITS 64
#endif

void F77_NAME(f_creatfile, F_CREATFILE)(char *fn, f_int *fh);
void F77_NAME(f_openfile, F_OPENFILE)(char *fn, f_int *fh);
void F77_NAME(f_deletefile,F_DELETEFILE)(char *fn, f_int *fh);
f_int F77_NAME(f_backupram, F_BACKUPRAM)(int *fhandle, f_int *diskloc,
                     f_int *blksize, double *buffer, f_int *fsize);
void F77_NAME(f_restoreram, F_RESTORERAM) (f_int *fhandle, f_int *diskloc, 
                  f_int *blksize, double *buffer, f_int *fsize);
void F77_NAME(f_close_file, F_CLOSE_FILE) (f_int *fh);
void F77_NAME(f_write_disk, F_WRITE_DISK) (f_int *fh, long long *startpos,
                                   double *buffer, f_int *fsize);
void F77_NAME(f_read_disk, F_READ_DISK) (f_int *fh, long long *startpos,
                                   double *buffer, f_int *fsize);

void F77_NAME(f_close_file, F_CLOSE_FILE) (f_int *fh)
{
   int handle = *fh;
   if (!handle) close(handle);
}

int backupram(int fh, long long startpos, double* buffer, 
               long size);

int restoreram(int fh, long long startpos, double* buffer, 
               long size);


int creatfile (char* filename, int *fh)
{
    int current=0;
    int filenumber=1;

    if( (*fh = open( (const char*)filename, O_CREAT|O_RDWR, S_IRUSR | 
                            S_IWUSR | S_IRGRP | S_IROTH)) == -1)
    {
        printf( "failed on creating input file %s, errno=%d, EEXIST=%d\n", 
            filename, errno, EEXIST );
        return -1;
    }
    return 1;
}

int openfile (char* filename, int *fh)
{
    int current=0;
    int filenumber=1;

    if( (*fh = open( (const char*)filename, O_RDWR, S_IRUSR | 
                            S_IWUSR | S_IRGRP | S_IROTH )) == -1)
    {
        printf( "failed on opening input file %s, errno=%d, EEXIST=%d\n", 
            filename, errno, EEXIST );
        return -1;
    }
    return 1;
}

void F77_NAME(f_creatfile, F_CREATFILE)(char *fn, f_int *fh)
{
   int cfh, ierr;
   ierr = creatfile(fn, &cfh);
   *fh = (f_int) cfh;
}

void F77_NAME(f_openfile, F_OPENFILE)(char* filename, f_int *fh)
{
    int handle;

    if( (handle = open( (const char*)filename, O_RDWR, S_IRUSR | 
                            S_IWUSR)) == -1)
    {
        printf( "failed on opening input file %s, errno=%d, EEXIST=%d\n", 
            filename, errno, EEXIST );
    }

    *fh = handle;
}

/*==========================================================================
copy double data from buffer with the size of size.
return -1 if can not open file
return 1 if successful.
==========================================================================*/
int backupram(int fh, long long startpos, double* buffer, 
              long size)
{
    int me;

#ifdef AIX
    if((lseek64(fh, startpos*sizeof(double), SEEK_SET)) < 0)
#else
#ifdef BLUEGENE
    if((lseek64(fh, startpos*sizeof(double), SEEK_SET)) < 0)
#else
    if((lseek(fh, startpos*sizeof(double), SEEK_SET)) < 0)
#endif
#endif
    {
        MPI_Comm_rank(MPI_COMM_WORLD, &me);
        printf("Server %d Backup: Error moving file pointer, errorno=%d, fh %d startpos %ld, size %ld\n", me,errno, fh, startpos, size);
        MPI_Abort(MPI_COMM_WORLD, 1);
    }
    if((write( fh, (char*)buffer, size*sizeof(double))) < 0 )
    {
        MPI_Comm_rank(MPI_COMM_WORLD, &me);
        printf("Server %d Backup: Error writing to file, errorno=%d\n", me, errno); 
        MPI_Abort(MPI_COMM_WORLD, 1);
    }
    return 1;
}

f_int F77_NAME(f_backupram, F_BACKUPRAM)(f_int *fhandle, f_int *diskloc,
                     f_int *blksize, double *buffer, f_int *fsize)
{
   int fh = *fhandle;
   long size = *fsize;
   long long startpos;
   long long loc, bsize;
   f_int rc;

   rc = 0;
   loc = *diskloc-1;
   bsize = *blksize;
   startpos = loc * bsize;
   if (startpos < 0) 
   {
      printf("Error: Integer overflow in f_backupram: startpos %lld\n",startpos);
      printf("       diskloc, fsize = %d %d\n",*diskloc, *fsize);
      rc = -1;
      return rc;
   }

   backupram(fh, startpos, buffer, size);

   return rc;
}

void F77_NAME(f_write_disk, F_WRITE_DISK) (f_int *fh, long long *startpos, 
                                   double *buffer, f_int *fsize)
{
   int size = *fsize;
   int handle = *fh;
   long long offset = *startpos;

   backupram(handle, offset, buffer, size);
}

void F77_NAME(f_write_disk_ints, F_WRITE_DISK_INTS) (f_int *fh, long long *startpos, 
                                   f_int *buffer, f_int *fsize)
{
   int size = *fsize;
   int handle = *fh;
   long long offset = *startpos*sizeof(f_int);

    int me;

#ifdef AIX
    if((lseek64(handle, offset, SEEK_SET)) < 0)
#else
#ifdef BLUEGENE
    if((lseek64(handle, offset, SEEK_SET)) < 0)
#else
    if((lseek(handle, offset, SEEK_SET)) < 0)
#endif
#endif
    {
        MPI_Comm_rank(MPI_COMM_WORLD, &me);
        printf("Server %d Backup: Error moving file pointer, errorno=%d, fh %d offset %ld, size %ld\n", me,errno, fh, offset, size);
        MPI_Abort(MPI_COMM_WORLD, 1);
    }
    if((write( handle, (char*)buffer, size*sizeof(double))) < 0 )
    {
        MPI_Comm_rank(MPI_COMM_WORLD, &me);
        printf("Server %d Backup: Error writing to file, errorno=%d\n", me, errno); 
        MPI_Abort(MPI_COMM_WORLD, 1);
    }
}

/*==========================================================================
restore file from disk to ram, 
make up by the file name:
/(tmpdirectory)/Asmgr<current>.tmp
<current> will be filled with 0 to the length of filenumber.
copy data of double type from buffer with the size of (int) size.
return -1 if can not open file
return 1 if successful.
==========================================================================*/
int restoreram(int fh, long long startpos, double* buffer, 
               long size)
{
    int me;

#ifdef AIX
    if((lseek64(fh, startpos*sizeof(double), SEEK_SET)) < 0)
#else
#ifdef BLUEGENE
    if((lseek64(fh, startpos*sizeof(double), SEEK_SET)) < 0)
#else
    if((lseek(fh, startpos*sizeof(double), SEEK_SET)) < 0)
#endif
#endif
    {
        MPI_Comm_rank(MPI_COMM_WORLD, &me);
        printf("Server %d Restore: Error move file pointer, errorno=%d, startpos %ld\n", me,errno,startpos);
        MPI_Abort(MPI_COMM_WORLD, 1);
    }
    if((read( fh, (char*)buffer, size*sizeof(double))) <0 )
    {
        MPI_Comm_rank(MPI_COMM_WORLD, &me);
        printf("Server %d Restore: Error reading from file, errorno=%d\n", me, errno); 
        MPI_Abort(MPI_COMM_WORLD, 1);
    }
    return 1;
}

void F77_NAME(f_restoreram, F_RESTORERAM) (f_int *fhandle, f_int *diskloc, 
                  f_int *blksize, double *buffer, f_int *fsize)
{
   int fh = *fhandle;
   long size = *fsize;
   long long startpos;
   long long loc, bsize;

   loc = *diskloc-1;
   bsize = *blksize;
   startpos = loc * bsize;

   restoreram(fh, startpos, buffer, size); 
}

void F77_NAME(f_read_disk, F_READ_DISK) (f_int *fh, long long *startpos, 
                                   double *buffer, f_int *fsize)
{
   long size = *fsize;
   int handle = *fh;
   long long offset = *startpos;

   restoreram(handle, offset, buffer, size);
}

void F77_NAME(f_read_disk_ints, F_READ_DISK_INTS) (f_int *fh, long long *startpos, 
                                   f_int *buffer, f_int *fsize)
{
   /* Size is in words, not bytes. */

   long size = *fsize;
   int handle = *fh;
   long long offset = *startpos*sizeof(f_int);
   int me; 

#ifdef AIX
    if((lseek64(handle, offset, SEEK_SET)) < 0)
#else
#ifdef BLUEGENE
    if((lseek64(handle, offset, SEEK_SET)) < 0)
#else
    if((lseek(handle, offset, SEEK_SET)) < 0)
#endif
#endif
    {
        MPI_Comm_rank(MPI_COMM_WORLD, &me);
        printf("Server %d f_read_disk_ints: Error move file pointer, errorno=%d, offset %ld\n", me,errno,offset);
        MPI_Abort(MPI_COMM_WORLD, 1);
    }
    if((read( handle, (char*)buffer, size*sizeof(f_int))) <0 )
    {
        MPI_Comm_rank(MPI_COMM_WORLD, &me);
        printf("Server %d Restore: Error reading from file, errorno=%d\n", me, errno);
        MPI_Abort(MPI_COMM_WORLD, 1);
    }

}

void deletefile (char *filename, int fh)
{
    if (fh >= 0) close(fh);
    if( remove( (const char* )filename) == -1 )
    {
        printf("Error in deleting %s, errno value is %d\n", filename, errno);
    }
}

void F77_NAME(f_deletefile,F_DELETEFILE)(char *fn, f_int *fh)
{
    int handle = *fh;

     deletefile(fn, handle);
}

void F77_NAME(f_renamefile, F_RENAMEFILE)(char *f1, char *f2)
{
   rename(f1, f2);
}

void F77_NAME(get_filelen, GET_FILELEN) (f_int *fh, long long *filelength)
{
     
   int handle = *fh;
   off_t offset = 0;
   off_t len;
#ifdef AIX
    len = lseek64(handle, offset, SEEK_END);
#else
#ifdef BLUEGENE
    len = lseek64(handle, offset, SEEK_END);
#else
    len = lseek(handle, offset, SEEK_END);
#endif
#endif

   *filelength = len;
}