1 #ifdef TNG_BUILD_OPENMP_EXAMPLES
3 /* This code is part of the tng binary trajectory format.
5 * Written by Magnus Lundborg
6 * Copyright (c) 2012-2013, The GROMACS development team.
7 * Check out http://www.gromacs.org for more information.
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the Revised BSD License.
16 #include "tng/tng_io.h"
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. */
24 int main(int argc, char **argv)
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;
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;
39 printf("No file specified\n");
41 printf("tng_parallel_read <tng_file> [particle number = %"PRId64"]\n",
46 // A reference must be passed to allocate memory
47 if(tng_trajectory_init(&traj) != TNG_SUCCESS)
49 tng_trajectory_destroy(&traj);
52 tng_input_file_set(traj, argv[1]);
54 tng_current_frame_set_get(traj, &frame_set);
56 // Read the file headers
57 tng_file_headers_read(traj, TNG_USE_HASH);
61 particle = strtoll(argv[2], 0, 10);
64 tng_num_frame_sets_get(traj, &n_frame_sets);
65 tng_num_frames_per_frame_set_get(traj, &n_frames_per_frame_set);
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--;)
71 /* Assume 3 values per frame even if it's not determined yet */
72 particle_pos[i] = malloc(sizeof(union data_values) * 3);
75 printf("%"PRId64" frame sets\n", n_frame_sets);
77 if(tng_atom_name_of_particle_nr_get(traj, particle, atom_name,
80 tng_residue_name_of_particle_nr_get(traj, particle, res_name,
84 printf("Particle: %s (%s)\n", atom_name, res_name);
88 printf("Particle name not found\n");
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)\
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);
104 for(i = 0; i < n_frame_sets; i++)
106 if(tng_frame_set_nr_find(local_traj, i) != TNG_SUCCESS)
108 printf("FAILED finding frame set %d!\n", i);
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)
116 printf("FAILED getting particle data\n");
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++)
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];
139 tng_particle_data_values_free(local_traj, local_positions, n_frames, n_particles,
140 n_values_per_frame, data_type);
142 tng_trajectory_destroy(&local_traj);
147 for(j = 0; j < tot_n_frames; j++)
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);
154 for(j = 0; j < tot_n_frames; j++)
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);
160 case TNG_DOUBLE_DATA:
161 for(j = 0; j < tot_n_frames; j++)
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);
171 /* Free more memory */
172 for(i = n_frame_sets * n_frames_per_frame_set; i--;)
174 free(particle_pos[i]);
178 tng_trajectory_destroy(&traj);