44e8f796b712762586fbea1376b6764cdabdacf6
[alexxy/gromacs.git] / src / external / tng_io / src / tests / tng_parallel_read.c
1 #ifdef TNG_BUILD_OPENMP_EXAMPLES
2
3 /* This code is part of the tng binary trajectory format.
4  *
5  * Written by Magnus Lundborg
6  * Copyright (c) 2012-2013, The GROMACS development team.
7  * Check out http://www.gromacs.org for more information.
8  *
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the Revised BSD License.
12  */
13
14 #include <stdlib.h>
15 #include <stdio.h>
16 #include "tng/tng_io.h"
17
18
19 /* N.B. this code is for testing parallel reading of trajectory frame sets. The
20  * performance is not improved very much and is to a large extent limited by
21  * disk i/o. It can however be used as inspiration for writing parallel code
22  * using the TNG library. The code is NOT fully tested and may behave strangely. */
23
24 int main(int argc, char **argv)
25 {
26     tng_trajectory_t traj, local_traj = 0;
27     union data_values ***local_positions = 0; // A 3-dimensional array to be populated
28     union data_values **particle_pos = 0;
29     int64_t n_particles, n_values_per_frame, n_frame_sets, n_frames;
30     int64_t n_frames_per_frame_set, tot_n_frames = 0;
31     char data_type;
32     int i, j, fail;
33     int64_t particle = 0, local_first_frame, local_last_frame;
34     char atom_name[64], res_name[64];
35     tng_trajectory_frame_set_t frame_set = 0;
36
37     if(argc <= 1)
38     {
39         printf("No file specified\n");
40         printf("Usage:\n");
41         printf("tng_parallel_read <tng_file> [particle number = %"PRId64"]\n",
42                particle);
43         exit(1);
44     }
45
46     // A reference must be passed to allocate memory
47     if(tng_trajectory_init(&traj) != TNG_SUCCESS)
48     {
49         tng_trajectory_destroy(&traj);
50         exit(1);
51     }
52     tng_input_file_set(traj, argv[1]);
53
54     tng_current_frame_set_get(traj, &frame_set);
55
56     // Read the file headers
57     tng_file_headers_read(traj, TNG_USE_HASH);
58
59     if(argc >= 3)
60     {
61         particle = strtoll(argv[2], 0, 10);
62     }
63
64     tng_num_frame_sets_get(traj, &n_frame_sets);
65     tng_num_frames_per_frame_set_get(traj, &n_frames_per_frame_set);
66
67     particle_pos = malloc(sizeof(union data_values *) * n_frame_sets *
68     n_frames_per_frame_set);
69     for(i = n_frame_sets * n_frames_per_frame_set; i--;)
70     {
71         /* Assume 3 values per frame even if it's not determined yet */
72         particle_pos[i] = malloc(sizeof(union data_values) * 3);
73     }
74
75     printf("%"PRId64" frame sets\n", n_frame_sets);
76
77     if(tng_atom_name_of_particle_nr_get(traj, particle, atom_name,
78                                         sizeof(atom_name)) ==
79        TNG_SUCCESS &&
80        tng_residue_name_of_particle_nr_get(traj, particle, res_name,
81                                            sizeof(res_name)) ==
82        TNG_SUCCESS)
83     {
84         printf("Particle: %s (%s)\n", atom_name, res_name);
85     }
86     else
87     {
88         printf("Particle name not found\n");
89     }
90
91     fail = 0;
92
93 #pragma omp parallel \
94 private (n_frames, n_particles, n_values_per_frame, \
95          local_first_frame, local_last_frame, j, fail) \
96 firstprivate (local_traj, local_positions, frame_set)\
97 shared(data_type, traj, n_frame_sets, particle_pos, particle, i, tot_n_frames)\
98 default(none)
99 {
100     /* Each tng_trajectory_t keeps its own file pointers and i/o positions.
101      * Therefore there must be a copy for each thread. */
102     tng_trajectory_init_from_src(traj, &local_traj);
103 #pragma omp for
104     for(i = 0; i < n_frame_sets; i++)
105     {
106         if(tng_frame_set_nr_find(local_traj, i) != TNG_SUCCESS)
107         {
108             printf("FAILED finding frame set %d!\n", i);
109             tot_n_frames = 0;
110             fail = 1;
111         }
112         if(tng_particle_data_get(local_traj, TNG_TRAJ_POSITIONS, &local_positions,
113                                  &n_frames, &n_particles, &n_values_per_frame,
114                                  &data_type) != TNG_SUCCESS)
115         {
116             printf("FAILED getting particle data\n");
117             tot_n_frames = 0;
118             fail = 1;
119         }
120         if(!fail)
121         {
122             tng_current_frame_set_get(local_traj, &frame_set);
123             tng_frame_set_frame_range_get(local_traj, frame_set, &local_first_frame, &local_last_frame);
124     //         printf("Frame %"PRId64"-%"PRId64":\n", local_first_frame, local_last_frame);
125     //         printf("%"PRId64" %"PRId64" %"PRId64"\n", n_frames, n_particles, n_values_per_frame);
126             tot_n_frames += n_frames;
127             for(j = 0; j < n_frames; j++)
128             {
129                 particle_pos[local_first_frame + j][0] = local_positions[j][particle][0];
130                 particle_pos[local_first_frame + j][1] = local_positions[j][particle][1];
131                 particle_pos[local_first_frame + j][2] = local_positions[j][particle][2];
132             }
133         }
134     }
135
136     // Free memory
137     if(local_positions)
138     {
139         tng_particle_data_values_free(local_traj, local_positions, n_frames, n_particles,
140                                       n_values_per_frame, data_type);
141     }
142     tng_trajectory_destroy(&local_traj);
143 }
144     switch(data_type)
145     {
146     case TNG_INT_DATA:
147         for(j = 0; j < tot_n_frames; j++)
148         {
149             printf("\t%"PRId64"\t%"PRId64"\t%"PRId64"\n", particle_pos[j][0].i,
150                    particle_pos[j][1].i, particle_pos[j][2].i);
151         }
152         break;
153     case TNG_FLOAT_DATA:
154         for(j = 0; j < tot_n_frames; j++)
155         {
156             printf("\t%f\t%f\t%f\n", particle_pos[j][0].f,
157                    particle_pos[j][1].f, particle_pos[j][2].f);
158         }
159         break;
160     case TNG_DOUBLE_DATA:
161         for(j = 0; j < tot_n_frames; j++)
162         {
163             printf("\t%f\t%f\t%f\n", particle_pos[j][0].d,
164                    particle_pos[j][1].d, particle_pos[j][2].d);
165         }
166         break;
167     default:
168         break;
169     }
170
171     /* Free more memory */
172     for(i = n_frame_sets * n_frames_per_frame_set; i--;)
173     {
174         free(particle_pos[i]);
175     }
176     free(particle_pos);
177
178     tng_trajectory_destroy(&traj);
179
180     return(0);
181 }
182
183 #endif