~ubuntu-branches/debian/stretch/adios/stretch

« back to all changes in this revision

Viewing changes to tests/suite/programs/steps_read_stream.c

  • Committer: Package Import Robot
  • Author(s): Alastair McKinstry
  • Date: 2014-06-16 23:06:38 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20140616230638-5a0z7ylxx8i0edrg
Tags: 1.7.0-1
* New upstream release.
* Add adios.pc pkgconfig file. adios_config now uses this.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Staged write example code.
 
2
   Assumptions:
 
3
     - one output step fits into the memory of the staged writer.
 
4
       Actually, this means, even more memory is needed than the size of output.
 
5
       We need to read each variable while also buffering all of them for output.
 
6
     - output steps contain the same variable set (no changes in variables)
 
7
     - attributes are the same for all steps (will write only once here)
 
8
*/
 
9
 
 
10
#include <stdio.h>
 
11
#include <stdlib.h>
 
12
#include <string.h>
 
13
#include <unistd.h>
 
14
#include <sys/types.h>
 
15
#include <sys/stat.h>
 
16
#include <fcntl.h>
 
17
#include <errno.h>
 
18
#include "mpi.h"
 
19
#include "adios_read.h"
 
20
#include "adios_error.h"
 
21
 
 
22
static const int max_read_buffer_size  = 1024*1024*1024;
 
23
static const int max_write_buffer_size = 1024*1024*1024;
 
24
 
 
25
static int timeout_sec = 30; // will stop if no data found for this time (-1: never stop)
 
26
enum ADIOS_READ_METHOD read_method = ADIOS_READ_METHOD_BP;
 
27
 
 
28
 
 
29
// Global variables
 
30
int         rank, numproc;
 
31
MPI_Comm    comm; 
 
32
ADIOS_FILE *f;      // stream for reading
 
33
int64_t    fh;     // ADIOS output file handle
 
34
int64_t     gh;     // ADIOS group for output definitions
 
35
uint64_t    write_total; // data size written by one processor
 
36
uint64_t    largest_block; // the largest variable block one process reads
 
37
char     ** group_namelist; // name of ADIOS group
 
38
char       *readbuf; // read buffer
 
39
int         decomp_values[10];
 
40
 
 
41
 
 
42
int process_metadata();
 
43
int read_records();
 
44
 
 
45
 
 
46
int main (int argc, char ** argv) 
 
47
{
 
48
    int         err;
 
49
    int         steps = 0, curr_step;
 
50
    int         retval = 0;
 
51
 
 
52
    MPI_Init (&argc, &argv);
 
53
    comm = MPI_COMM_WORLD;
 
54
    MPI_Comm_rank (comm, &rank);
 
55
    MPI_Comm_size (comm, &numproc);
 
56
 
 
57
    
 
58
    char infilename[] = "steps.bp";
 
59
 
 
60
    err = adios_read_init_method(read_method, comm, 
 
61
                                 "max_chunk_size=100; "
 
62
                                 "app_id =32767; \n"
 
63
                                 "verbose= 3;"
 
64
                                 "poll_interval  =  100;"
 
65
                                );
 
66
 
 
67
    if (!err) {
 
68
        printf ("%s\n", adios_errmsg());
 
69
    }
 
70
 
 
71
 
 
72
    printf ("Waiting to open stream %s...\n", infilename);
 
73
    f = adios_read_open_stream (infilename, read_method, comm,
 
74
            ADIOS_LOCKMODE_ALL, timeout_sec);
 
75
    if (adios_errno == err_file_not_found) 
 
76
    {
 
77
        printf ("rank %d: Stream not found after waiting %d seconds: %s\n", 
 
78
               rank, timeout_sec, adios_errmsg());
 
79
        retval = adios_errno;
 
80
    } 
 
81
    else if (adios_errno == err_end_of_stream) 
 
82
    {
 
83
        printf ("rank %d: Stream terminated before open. %s\n", rank, adios_errmsg());
 
84
        retval = adios_errno;
 
85
    } 
 
86
    else if (f == NULL) {
 
87
        printf ("rank %d: Error at opening stream: %s\n", rank, adios_errmsg());
 
88
        retval = adios_errno;
 
89
    } 
 
90
    else 
 
91
    {
 
92
        // read data here... 
 
93
        while(1) {
 
94
            steps++; // start counting from 1
 
95
 
 
96
            /*
 
97
            printf ("File info:\n");
 
98
            printf ("  current step:   %d\n", f->current_step);
 
99
            printf ("  last step:      %d\n", f->last_step);
 
100
            printf ("  # of variables: %d:\n", f->nvars);
 
101
            */
 
102
 
 
103
            if (steps==1)
 
104
                retval = process_metadata();
 
105
 
 
106
            retval = read_records();
 
107
 
 
108
            // advance to 1) next available step with 2) blocking wait 
 
109
            curr_step = f->current_step; // save for final bye print
 
110
            adios_advance_step (f, 0, 0.0);
 
111
 
 
112
            if (adios_errno == err_end_of_stream)
 
113
            {
 
114
                break; // quit while loop
 
115
            }
 
116
            else if (adios_errno == err_step_notready)
 
117
            {
 
118
                printf ("rank %d: No new step arrived within the timeout. Quit. %s\n",
 
119
                        rank, adios_errmsg());
 
120
                break; // quit while loop
 
121
            }
 
122
            else if (f->current_step != curr_step+1)
 
123
            {
 
124
                // we missed some steps
 
125
                printf ("rank %d: WARNING: steps %d..%d were missed when advancing.\n",
 
126
                        rank, curr_step+1, f->current_step-1);
 
127
            }
 
128
 
 
129
 
 
130
        }
 
131
        adios_read_close (f);
 
132
    } 
 
133
 
 
134
    adios_read_finalize_method (read_method);
 
135
    MPI_Finalize ();
 
136
 
 
137
    return retval;
 
138
}
 
139
 
 
140
 
 
141
typedef struct {
 
142
    ADIOS_VARINFO * v;
 
143
    uint64_t        start[10];
 
144
    uint64_t        count[10];
 
145
    uint64_t        writesize; // size of subset this process writes, 0: do not write
 
146
} VarInfo;
 
147
 
 
148
VarInfo * varinfo;
 
149
 
 
150
int NX, Width, nblocks;
 
151
 
 
152
int process_metadata()
 
153
{
 
154
    int retval = 0;
 
155
    int i, j;
 
156
    char gdims[256], ldims[256], offs[256];
 
157
    uint64_t sum_count;
 
158
    ADIOS_VARINFO *v; // shortcut pointer
 
159
 
 
160
    /* First step processing */
 
161
 
 
162
    printf ("Get info on variable Width\n"); 
 
163
    v = adios_inq_var (f, "Width");
 
164
    if (v == NULL) {
 
165
        printf ("rank %d: ERROR: Variable %s inquiry failed: %s\n", 
 
166
                rank, "Width", adios_errmsg());
 
167
        return 1;
 
168
    }
 
169
    Width = *(int *)v->value;
 
170
    adios_free_varinfo (v);
 
171
    printf ("rank %d: Width = %d\n", rank, Width); 
 
172
 
 
173
    printf ("Get info on variable NX\n"); 
 
174
    v = adios_inq_var (f, "NX");
 
175
    if (v == NULL) {
 
176
        printf ("rank %d: ERROR: Variable %s inquiry failed: %s\n", 
 
177
                rank, "NX", adios_errmsg());
 
178
        return 1;
 
179
    }
 
180
    NX = *(int *)v->value;
 
181
    adios_free_varinfo (v);
 
182
    printf ("rank %d: NX = %d\n", rank, NX); 
 
183
 
 
184
    printf ("Get info on variable record\n"); 
 
185
    v = adios_inq_var (f, "record");
 
186
    if (v == NULL) {
 
187
        printf ("rank %d: ERROR: Variable %s inquiry failed: %s\n", 
 
188
                rank, "record", adios_errmsg());
 
189
        return 1;
 
190
    }
 
191
    nblocks = v->nblocks[0];
 
192
    printf ("rank %d: record dims = %llu * %llu \n", rank, v->dims[0], v->dims[1]); 
 
193
    adios_free_varinfo (v);
 
194
    printf ("rank %d: nblocks = %d\n", rank, nblocks); 
 
195
 
 
196
    return retval;
 
197
}
 
198
 
 
199
int read_records()
 
200
{
 
201
    int retval = 0;
 
202
    int i,j;
 
203
 
 
204
    int N = nblocks/numproc;
 
205
    int startidx=N*rank;
 
206
 
 
207
    char *text;
 
208
    text = malloc (Width*NX+1);
 
209
    text[Width*NX] = 0;
 
210
    
 
211
    for (i=0; i<N; i++) 
 
212
    {
 
213
        memset (text, '+', Width*NX);
 
214
        // read one block of records
 
215
        //printf ("rank %d: Read block %d\n", rank, startidx+i); 
 
216
        ADIOS_SELECTION *sel = adios_selection_writeblock (startidx+i);
 
217
        adios_schedule_read (f, sel, "record", 0, 1, text);
 
218
        adios_perform_reads (f, 1);   
 
219
        adios_selection_delete (sel);
 
220
 
 
221
        printf ("block %2d = ", startidx+i);
 
222
        for (j=0; j<NX; j++) 
 
223
            printf ("[%s]", text+j*Width);
 
224
        printf ("\n");
 
225
    }
 
226
 
 
227
    return retval;
 
228
}
 
229
 
 
230
 
 
231