h5t_io.c 80.5 KB
Newer Older
1
/*
gsell's avatar
gsell committed
2
  Copyright (c) 2006-2016, The Regents of the University of California,
3 4 5 6 7 8 9
  through Lawrence Berkeley National Laboratory (subject to receipt of any
  required approvals from the U.S. Dept. of Energy) and the Paul Scherrer
  Institut (Switzerland).  All rights reserved.

  License: see file COPYING in top level of source distribution.
*/

gsell's avatar
gsell committed
10
#include "private/h5_types.h"
gsell's avatar
gsell committed
11 12

#include <stdlib.h>
13 14
#include <unistd.h> // for the use of sleep

gsell's avatar
gsell committed
15
#ifdef WITH_PARALLEL_H5GRID
16 17 18
#include <parmetis.h>
#endif

gsell's avatar
gsell committed
19 20
#include "private/h5_attribs.h"
#include "private/h5_hdf5.h"
21

gsell's avatar
gsell committed
22 23 24 25
#include "private/h5_model.h"
#include "private/h5t_types.h"
#include "private/h5t_access.h"
#include "private/h5t_core.h"
26 27 28 29 30 31 32 33 34 35 36 37 38
#include "h5core/h5t_map.h"

int preferred_direction = 0;
// 0 x direction
// 1 y direction
// 2 z direction

int dont_use_parmetis = 0;
// 0 use parmetis
// 1 distribute with morton ordering
// 2 distribute geometrically with preferred direction


gsell's avatar
gsell committed
39 40 41 42 43 44 45 46
#include "private/h5t_types.h"
#include "private/h5t_model.h"
#include "private/h5t_map.h"
#include "private/h5t_adjacencies.h"
#include "private/h5t_io.h"
#include "private/h5t_store.h"
#include "private/h5t_core.h"
#include "private/h5_mpi.h"
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

#include <assert.h>

static hid_t
open_space_all (
		h5t_mesh_t* const m,
        const hid_t dataset_id
        ) {
	UNUSED_ARGUMENT (m);
	UNUSED_ARGUMENT (dataset_id);
	return H5S_ALL;
}

int
hidxmap_cmp (
        const void* __a,
        const void* __b
        ) {
	h5_idxmap_el_t* a = (h5_idxmap_el_t*)__a;
	h5_idxmap_el_t* b = (h5_idxmap_el_t*)__b;
	return b->glb_idx - a->glb_idx;
}

unsigned int
hidxmap_compute_hval (
        const void* __item
        ) {
	h5_idxmap_el_t* item = (h5_idxmap_el_t*)__item;
	uint16_t* key =  (uint16_t*)&item->glb_idx;
	unsigned int count = sizeof (item->glb_idx) / sizeof (uint16_t);
	unsigned int hval = count;
	while (count--) {
		if (*key) {
			hval <<= 6;
			hval += *key;
		}
		key++;
	}
	return hval;
}

/*
   Write vertices:
 * either we write a new dataset
 * or we append data to this dataset
 * appending means, a new level has been added
 * existing vertices will never be changed!

 */
static h5_err_t
write_vertices (
        h5t_mesh_t* const m
        ) {
	H5_PRIV_FUNC_ENTER (h5_err_t, "m=%p", m);
	assert (m->num_leaf_levels > 0);

	// quick hack for serial case (for the time being writes are serial anyway)
	for (size_t i = 0; i < m->num_leaf_levels; i++) {
		m->num_glb_vertices[i] = m->num_loc_vertices[i];
	}

	m->dsinfo_vertices.dims[0] = m->num_loc_vertices[m->num_leaf_levels-1];
	TRY( h5priv_write_dataset_by_name (
				 m,
	             m->f,
	             m->mesh_gid,
	             &m->dsinfo_vertices,
	             open_space_all,
	             open_space_all,
	             m->vertices) );

	TRY (h5priv_write_attrib (
	             m->mesh_gid,
	             "__num_vertices__",
gsell's avatar
gsell committed
121
	             H5_INT64_T,
122
	             m->num_glb_vertices,
gsell's avatar
gsell committed
123
	             m->num_leaf_levels));
124 125 126
	TRY (h5priv_write_attrib (
	             m->mesh_gid,
	             "__num_b_vertices__",
gsell's avatar
gsell committed
127
	             H5_INT64_T,
128
	             m->num_b_vtx,
gsell's avatar
gsell committed
129
	             m->num_leaf_levels));
130 131 132
	TRY (h5priv_write_attrib (
	             m->mesh_gid,
	             "__first_b_vertices__",
gsell's avatar
gsell committed
133
	             H5_INT64_T,
134
	             m->first_b_vtx,
gsell's avatar
gsell committed
135
	             m->num_leaf_levels));
gsell's avatar
gsell committed
136
	H5_RETURN (H5_SUCCESS);
137
}
gsell's avatar
gsell committed
138
#ifdef WITH_PARALLEL_H5GRID
139

gsell's avatar
gsell committed
140
static h5_err_t
141
add_chunk_to_list (
gsell's avatar
gsell committed
142 143 144 145
	h5t_mesh_t* const m,
	h5_loc_idxlist_t** list,
	h5_oct_idx_t oct_idx
	) {
146 147 148 149 150 151 152 153
	H5_PRIV_FUNC_ENTER (h5_err_t, "m=%p, list=%p, oct_idx=%d", m, list, oct_idx);
	h5t_oct_userdata_t* userdata;
	TRY (H5t_get_userdata_r (m->octree, oct_idx,(void **) &userdata));
		for (int i = 0; i < OCT_USERDATA_SIZE; i++) {
			if (userdata->idx[i] > -1) {
				h5priv_search_in_loc_idxlist(list, (h5_loc_idx_t)userdata->idx[i]);
			}
		}
gsell's avatar
gsell committed
154
	H5_RETURN (H5_SUCCESS);
155
}
gsell's avatar
gsell committed
156 157 158 159 160 161 162 163

#if 0
static int
compare_chk_idx (
	const void *p_a,
	const void *p_b
	) {
	return ((*(h5_chk_idx_t*)p_a) - (*(h5_chk_idx_t*)p_b));
164
}
gsell's avatar
gsell committed
165
#endif
166 167 168

h5_err_t
h5tpriv_get_list_of_chunks_to_write (
gsell's avatar
gsell committed
169 170 171 172 173
	h5t_mesh_t* const m,
	h5_chk_idx_t** list,
	int* counter
	) {
	H5_PRIV_API_ENTER (h5_err_t, "m=%p list=%p, counter=%p", m, list, counter);
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
	h5_chk_idx_t num_chunks = m->chunks->curr_idx + 1; // Is that ok? yes if update is correct

	int rank = m->f->myproc;
	int size_list = num_chunks;
	TRY ( *list = h5_calloc (num_chunks, sizeof (**list)));
	*counter = 0;
	h5_loc_idxlist_t* loc_list = NULL; //WARNING works only if chk_idx == loc_idx
	TRY (h5priv_alloc_loc_idxlist (&loc_list, size_list));

	// go through chunks and get those that belong to this proc
	for (h5_loc_idx_t i = 0; i < num_chunks; i++) {
		if (H5t_get_proc (m->octree, m->chunks->chunks[i].oct_idx) == rank ) {
			TRY (h5priv_search_in_loc_idxlist (&loc_list, i));
		}
	}
	*counter = loc_list->num_items;
	for (int i = 0; i< loc_list->num_items; i++) {
		(*list)[i] = (h5_chk_idx_t)loc_list->items[i];
	}
//	memcpy (*list, loc_list->items, sizeof(**list) * loc_list->num_items);
	TRY (h5priv_free_loc_idxlist (&loc_list));

	if (size_list < *counter) {
		h5_debug ("Overflow of list_of_chunks");
gsell's avatar
gsell committed
198
		H5_LEAVE (H5_ERR_INTERNAL);
199
	}
gsell's avatar
gsell committed
200
	H5_RETURN (H5_SUCCESS);
201
}
gsell's avatar
gsell committed
202

203 204
static h5_err_t
exchange_g2l_vtx_map (
gsell's avatar
gsell committed
205 206 207 208 209
	h5t_mesh_t* const m,
	h5_idxmap_t* map,
	h5_glb_idx_t** range,
	h5_glb_idx_t** glb_vtx
	) {
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
	H5_PRIV_FUNC_ENTER (h5_err_t, "m=%p map=%p, range=%p, glb_vtx=%p", m, map, range, glb_vtx);
	// alloc/get range
	TRY (*range = h5_calloc (m->f->nprocs + 1, sizeof (**range)));
	TRY (h5tpriv_get_ranges (m,*range, map->num_items, 0));

	//alloc glb_vtx
	TRY (*glb_vtx = h5_calloc ((*range)[m->f->nprocs], sizeof (**glb_vtx)));
	h5_glb_idx_t* sendbuf = NULL;
	TRY (sendbuf = h5_calloc (map->num_items, sizeof (*sendbuf)));
	int* recvcount = NULL;
	int* recvdisp = NULL;
	TRY (recvcount = h5_calloc (m->f->nprocs , sizeof (*recvcount)));
	TRY (recvdisp = h5_calloc (m->f->nprocs , sizeof (*recvdisp)));
	for (int i = 0; i < m->f->nprocs; i++) {
		recvdisp[i] = (int) (*range)[i];
		recvcount[i] = (int) ((*range)[i+1] - (*range)[i]);
	}
	for (int i = 0; i < map->num_items; i++) {
		sendbuf[i] = map->items[i].glb_idx;
	}

	TRY (h5priv_mpi_allgatherv (
				sendbuf,
				(int)map->num_items,
				MPI_LONG,
				*glb_vtx,
				recvcount,
				recvdisp,
				MPI_LONG,
				m->f->props->comm));
gsell's avatar
gsell committed
240
	H5_RETURN (H5_SUCCESS);
241
}
gsell's avatar
gsell committed
242
#endif
243 244 245 246 247

/*
 * instead of bsearch it returns the first element that fulfills compare(key,element) == 0 in an unsorted array
 * we don't want to sort array since it's also containing a permutation
 */
gsell's avatar
gsell committed
248 249 250 251 252 253 254 255
void*
linsearch (
	const void *key,
	void *array,
	size_t count,
	size_t size,
	comparison_fn_t compare
	) {
256 257 258 259 260 261 262 263 264 265 266
	void* pointer = array;
	for (int i = 0; i < count; i++) {
		if (compare.compare(key,pointer) == 0) {
			return pointer;
			break;
		}
		pointer += size;
	}
	return NULL;
}

267 268 269 270 271 272 273 274 275
#if defined(WITH_PARALLEL_H5GRID)
static int
sort_glb_idx (
	const void *p_a,
	const void *p_b
	) {
	return (*(h5_glb_idx_t*)p_a) - (*(h5_glb_idx_t*)p_b);
}

gsell's avatar
gsell committed
276
static h5_err_t
277
remove_item_from_idxmap (
gsell's avatar
gsell committed
278 279 280
	h5_idxmap_t* map,
	int item_idx
	) {
281 282 283 284 285
	H5_PRIV_FUNC_ENTER (h5_err_t, "map=%p, item_idx=%d", map, item_idx);
	assert(item_idx < map->num_items);

	memmove(&map->items[item_idx], &map->items[item_idx + 1], (map->num_items - 1 - item_idx) * sizeof (*map->items));
	map->num_items--;
gsell's avatar
gsell committed
286
	H5_RETURN (H5_SUCCESS);
287
}
gsell's avatar
gsell committed
288

289 290 291 292 293 294
/*
 * Check if any proc with lower rank already writes a vtx that this proc has planed to write
 * if so remove it from the map. Only the proc with the lowest rank writes the vertex
 */
static h5_err_t
check_multiple_vtx_writes (
gsell's avatar
gsell committed
295 296 297 298 299
	h5t_mesh_t* const m,
	h5_idxmap_t* map,
	h5_glb_idx_t* range,
	h5_glb_idx_t* glb_vtx
	) {
300 301
	H5_PRIV_FUNC_ENTER (h5_err_t, "m=%p map=%p, range=%p, glb_vtx=%p", m, map, range, glb_vtx);
	if (m->f->myproc == 0) {
gsell's avatar
gsell committed
302
		H5_LEAVE (H5_SUCCESS);
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
	}
	h5_glb_idx_t num_glb_vtx = range[m->f->myproc];
	// sort glb_vtx up to my vtx
	qsort (glb_vtx,num_glb_vtx, sizeof (*glb_vtx), sort_glb_idx);

	h5_glb_idx_t* key = NULL;

//	comparison_fn_t comp_func;
//	comp_func.compare = sort_glb_idx;
	for (int i = 0; i < map->num_items; i++) {
		h5_glb_idx_t* retval = NULL;
		key = &map->items[i].glb_idx;
		retval = bsearch (key, glb_vtx, num_glb_vtx, sizeof (*glb_vtx), sort_glb_idx);
		if (retval != NULL) {
			// vertex already exist on proc with lower rank
			TRY (remove_item_from_idxmap (map, i));
			i--; // same position should be check again
		}
	}
gsell's avatar
gsell committed
322
	H5_RETURN (H5_SUCCESS);
323
}
gsell's avatar
gsell committed
324
#endif
325 326

h5_int32_t
gsell's avatar
gsell committed
327 328 329 330 331
h5priv_find_proc_to_write (
	h5t_mesh_t* const m,
	h5_loc_idx_t elem_idx
	) {
	H5_PRIV_API_ENTER (int, "m=%p ", m);
gsell's avatar
gsell committed
332
#ifdef WITH_PARALLEL_H5GRID
333 334 335 336
	h5_glb_idx_t glb_idx = h5tpriv_get_loc_elem_glb_idx (m, elem_idx);
	for (int i = 0; i < m->chunks->num_alloc; i++) {
		if ( glb_idx >= m->chunks->chunks[i].elem &&
				glb_idx < m->chunks->chunks[i].elem + m->chunks->chunks[i].num_elems) {
gsell's avatar
gsell committed
337
			H5_LEAVE (H5t_get_proc (m->octree, m->chunks->chunks[i].oct_idx));
338 339 340
		}
	}
#endif
gsell's avatar
gsell committed
341
	H5_RETURN (H5_SUCCESS);
342
}
gsell's avatar
gsell committed
343 344

#ifdef WITH_PARALLEL_H5GRID
345 346 347 348 349 350 351 352 353
/*
 * function returns a map with all the vtx that should be written by this proc
 *
 * first get all vtx that span elems that are written by this proc
 *
 * exchange the map and find which ones have to be written locally
 */
static h5_err_t
get_map_vertices_write (
gsell's avatar
gsell committed
354 355 356
	h5t_mesh_t* const m,
	h5_idxmap_t* map
	) {
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
	H5_PRIV_FUNC_ENTER (h5_err_t, "m=%p, map=%p", m, map);
	h5_chk_idx_t* list_of_chunks;
	int num_chunks = 0;
	TRY (h5tpriv_get_list_of_chunks_to_write (m, &list_of_chunks, &num_chunks));

	/* for the time being we use the hash table only for a fast test
	   whether a global index has already been added to the map or not. */
	h5_hashtable_t htab;
	TRY (h5priv_hcreate ((map->size << 2) / 3, &htab,
	                     hidxmap_cmp, hidxmap_compute_hval, NULL));

	int num_vertices = h5tpriv_ref_elem_get_num_vertices (m);

	// go through chunks
	for (int i = 0; i < num_chunks; i++) {
		h5_chk_idx_t chk_idx = list_of_chunks[i];
		h5_glb_idx_t glb_elem_idx = m->chunks->chunks[chk_idx].elem;
		h5_chk_size_t num_elem = m->chunks->chunks[chk_idx].num_elems;
		for (int j = 0; j < num_elem; j++) { // go through elements
			h5_loc_idx_t loc_elem_idx = h5t_map_glb_elem_idx2loc(m, glb_elem_idx + j);
			h5_loc_idx_t* vertices = h5tpriv_get_loc_elem_vertex_indices(m, loc_elem_idx);
			for (int k = 0; k < num_vertices; k++) {
				// get glb vertices
				h5_glb_idx_t glb_vidx = m->vertices[vertices[k]].idx;

				// add index temporarly to map ...
				map->items[map->num_items] = (h5_idxmap_el_t) {glb_vidx, vertices[k]};
				// ... and check whether it has already been added
				h5_idxmap_el_t* retval;
				h5priv_hsearch (&map->items[map->num_items],
				                H5_ENTER, (void**)&retval, &htab);
				if (retval == &map->items[map->num_items]) {
					// new entry in hash table thus in map
					map->num_items++;
				}

			}
		}
	}
	TRY (h5priv_hdestroy (&htab));
	h5priv_sort_idxmap (map);

	h5_glb_idx_t* range = NULL;
	h5_glb_idx_t* glb_vtx = NULL;
	// do exchange map
	TRY (exchange_g2l_vtx_map(m, map, &range, &glb_vtx));

	// check your vertices if they already appear on a proc with lower rank
	// if they appear, delete them from this map
	TRY (check_multiple_vtx_writes (m, map, range, glb_vtx));


	h5priv_sort_idxmap (map);
	TRY (h5_free (range));
	TRY (h5_free (glb_vtx));
gsell's avatar
gsell committed
412

gsell's avatar
gsell committed
413
	H5_RETURN (H5_SUCCESS);
414 415 416 417 418 419 420 421 422 423
}

//TODO maybe use ifdef to have name without _chk
static h5_err_t
write_vertices_chk (
        h5t_mesh_t* const m
        ) {
	H5_PRIV_FUNC_ENTER (h5_err_t, "m=%p", m);
	assert (m->num_leaf_levels > 0);
	hid_t dset_id;
424
	TRY (dset_id = hdf5_open_dataset_by_name (m->mesh_gid, m->dsinfo_vertices.name));
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
	hid_t mspace_id;
	hid_t dspace_id;


	h5_idxmap_t map_r;
	TRY (h5priv_new_idxmap (&map_r, m->num_loc_vertices[m->num_leaf_levels-1] + 128));
	h5_idxmap_t* map = &map_r;
	get_map_vertices_write (m, map);
	TRY (h5priv_mpi_barrier (m->f->props->comm));
	m->timing.measure[m->timing.next_time++] = MPI_Wtime();
	// create memspace
	hsize_t num_loc_vertices = m->num_loc_vertices[m->leaf_level];
	TRY (mspace_id = hdf5_create_dataspace(1, &num_loc_vertices, NULL));
	// add memspace
	hsize_t hstride = 1;
	H5S_seloper_t seloper = H5S_SELECT_SET; // first selection
	for (hsize_t i = 0; i < map->num_items; i++) {
		hsize_t hstart = map->items[i].loc_idx;
		hsize_t hcount = 1;
		while (map->items[i].loc_idx+1 == map->items[i+1].loc_idx &&
			       i+1 < map->num_items) {// WARINIG make sure +1 is right here
				i++; hcount++;
		}
		TRY (hdf5_select_hyperslab_of_dataspace (
			             mspace_id,
			             seloper,
			             &hstart, &hstride, &hcount,
			             NULL));
		seloper = H5S_SELECT_OR;
	}

	// create diskspace and select subset
	hsize_t num_glb_vertices = m->num_glb_vertices[m->num_leaf_levels-1];
	m->dsinfo_vertices.dims[0] = num_glb_vertices;
	TRY (dspace_id = hdf5_get_dataset_space (dset_id));
	TRY (hdf5_set_dataset_extent (dset_id, &num_glb_vertices));
	H5Sset_extent_simple(dspace_id, 1,m->dsinfo_vertices.dims, NULL); //TODO WRITE WRAPPER

	seloper = H5S_SELECT_SET; // first selection
	for (hsize_t i = 0; i < map->num_items; i++) {
		hsize_t hstart = map->items[i].glb_idx;
		hsize_t hcount = 1;
		while (map->items[i].glb_idx+1 == map->items[i+1].glb_idx &&
			       i+1 < map->num_items) { // WARINIG make sure +1 is right here
				i++; hcount++;
		}
		TRY (hdf5_select_hyperslab_of_dataspace (
			             dspace_id,
			             seloper,
			             &hstart, &hstride, &hcount,
			             NULL));
		seloper = H5S_SELECT_OR;
	}
	TRY (h5priv_mpi_barrier (m->f->props->comm));
	m->timing.measure[m->timing.next_time++] = MPI_Wtime();
	TRY (h5priv_start_throttle (m->f));

	TRY( h5priv_write_dataset_by_name_id (
	             m->f,
	             m->mesh_gid,
	             &m->dsinfo_vertices,
	             dset_id,
	             mspace_id,
	             dspace_id,
	             m->vertices) );


	TRY (h5priv_write_attrib (
	             m->mesh_gid,
	             "__num_vertices__",
gsell's avatar
gsell committed
495
	             H5_INT64_T,
496
	             m->num_glb_vertices,
gsell's avatar
gsell committed
497
	             m->num_leaf_levels));
498 499 500
	TRY (h5priv_write_attrib (
	             m->mesh_gid,
	             "__num_b_vertices__",
gsell's avatar
gsell committed
501
	             H5_INT64_T,
502
	             m->num_b_vtx,
gsell's avatar
gsell committed
503
	             m->num_leaf_levels));
504 505 506
	TRY (h5priv_write_attrib (
	             m->mesh_gid,
	             "__first_b_vertices__",
gsell's avatar
gsell committed
507
	             H5_INT64_T,
508
	             m->first_b_vtx,
gsell's avatar
gsell committed
509
	             m->num_leaf_levels));
510 511 512 513 514 515

	TRY (h5priv_end_throttle (m->f));
	TRY (hdf5_close_dataspace (dspace_id));
	TRY (hdf5_close_dataspace (mspace_id));
	TRY (hdf5_close_dataset (dset_id));
	m->f->empty = 0;
gsell's avatar
gsell committed
516

gsell's avatar
gsell committed
517
	H5_RETURN (H5_SUCCESS);
518
}
gsell's avatar
gsell committed
519

520 521 522 523 524
static h5_err_t
write_elems (
        h5t_mesh_t* const m
        ) {
	H5_PRIV_FUNC_ENTER (h5_err_t, "m=%p", m);
gsell's avatar
gsell committed
525
	H5_RETURN (h5_error_not_implemented ());
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
}

#else

// SERIAL WRITE
static h5_err_t
write_elems (
        h5t_mesh_t* const m
        ) {
	H5_PRIV_FUNC_ENTER (h5_err_t, "m=%p", m);
	assert (m->num_leaf_levels > 0);

	h5_loc_idx_t num_interior_elems = m->num_interior_elems[m->num_leaf_levels-1];
	// alloc and inititalize data in memory
	h5_glb_elem_t* glb_elems;
	TRY (glb_elems = h5tpriv_alloc_glb_elems (m, num_interior_elems));
	TRY (h5tpriv_init_glb_elems_struct (m, glb_elems));

	m->dsinfo_elems.dims[0] = num_interior_elems;
	TRY (h5priv_write_dataset_by_name (
				 m,
	             m->f,
	             m->mesh_gid,
	             &m->dsinfo_elems,
	             open_space_all,
	             open_space_all,
	             glb_elems));

	TRY (h5priv_write_attrib (
	             m->mesh_gid,
	             "__num_elems__",
gsell's avatar
gsell committed
557
	             H5_INT64_T,
558
	             m->num_glb_elems,
gsell's avatar
gsell committed
559
	             m->num_leaf_levels));
560 561 562 563

	TRY (h5priv_write_attrib (
	             m->mesh_gid,
	             "__num_leaf_elems__",
gsell's avatar
gsell committed
564
	             H5_INT64_T,
565
	             m->num_glb_leaf_elems,
gsell's avatar
gsell committed
566
	             m->num_leaf_levels));
567 568 569 570

	TRY (h5priv_write_attrib (
	             m->mesh_gid,
	             "__num_leaf_levels__",
gsell's avatar
gsell committed
571
	             H5_INT16_T,
572 573 574 575 576 577
	             &m->num_leaf_levels,
	             1));

	TRY (h5priv_write_attrib (
	             m->mesh_gid,
	             "__is_chunked__",
gsell's avatar
gsell committed
578
	             H5_INT16_T,
579 580 581 582 583
	             &m->is_chunked,
	             1));
	TRY (h5priv_write_attrib (
	             m->mesh_gid,
	             "__num_weights__",
gsell's avatar
gsell committed
584
	             H5_INT32_T,
585 586 587 588
	             &m->num_weights,
	             1));
	// release mem
	TRY (h5_free (glb_elems));
gsell's avatar
gsell committed
589
	H5_RETURN (H5_SUCCESS);
590 591
}
#endif
gsell's avatar
gsell committed
592
#ifdef WITH_PARALLEL_H5GRID
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
//TODO maybe use ifdef to have name without _chk
static h5_err_t
write_elems_chk (
        h5t_mesh_t* const m
        ) {
	H5_PRIV_FUNC_ENTER (h5_err_t, "m=%p", m);
	assert (m->num_leaf_levels > 0);

	h5_chk_idx_t* chk_list = NULL;
	int num_chk = 0;
	// get my chunks to write
	TRY (h5tpriv_get_list_of_chunks_to_write(m, &chk_list, &num_chk));

	hsize_t num_elems = 0;
	for (int i = 0; i < num_chk; i++) {
		num_elems += m->chunks->chunks[chk_list[i]].num_elems;
	}
	// alloc and inititalize data in memory
	h5_glb_elem_t* glb_elems;
	TRY (glb_elems = h5tpriv_alloc_glb_elems (m, num_elems));
	TRY (h5tpriv_init_glb_elems_struct_chk (m, glb_elems, chk_list, num_chk));

	// could check here that glb_elems are in correct order

	hid_t dset_id;
618
	TRY (dset_id = hdf5_open_dataset_by_name (m->mesh_gid, m->dsinfo_elems.name));
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
	hid_t dspace_id;
	hid_t mspace_id;
	hsize_t hstart = 0;
	hsize_t hstride = 1;
	hsize_t hcount = num_elems;
	TRY (mspace_id = hdf5_create_dataspace (1, &num_elems, NULL));
	TRY (hdf5_select_hyperslab_of_dataspace (
				             mspace_id,
				             H5S_SELECT_SET,
				             &hstart, &hstride, &hcount,
				             NULL));


	// create diskspace and select subset
	hsize_t num_glb_elems = m->num_glb_elems[m->leaf_level];
	m->dsinfo_elems.dims[0] = num_glb_elems;
	TRY (dspace_id = hdf5_get_dataset_space (dset_id));
	TRY (hdf5_set_dataset_extent (dset_id, &num_glb_elems));
	H5Sset_extent_simple(dspace_id, 1,m->dsinfo_elems.dims, NULL); //TODO WRITE WRAPPER


	hsize_t hnext = h5tpriv_get_glb_elem_idx(m, glb_elems, 0);
	hsize_t hcurr = 0;
	// with those two variables the number of func calls can be reduced 3 times!
	H5S_seloper_t seloper = H5S_SELECT_SET; // first selection
	for (hsize_t i = 0; i < num_elems; i++) {
		hstart = hnext;
		hcurr = hnext;
		hcount = 1;
		while (i + 1 < num_elems &&
				(hnext = h5tpriv_get_glb_elem_idx(m, glb_elems, i + 1)) == hcurr + 1) {
				i++; hcount++; hcurr++;
		}
		TRY (hdf5_select_hyperslab_of_dataspace (
			             dspace_id,
			             seloper,
			             &hstart, &hstride, &hcount,
			             NULL));
		seloper = H5S_SELECT_OR;
	}
	// TODO add throttle

	TRY (h5priv_write_dataset_by_name_id(
	             m->f,
	             m->mesh_gid,
	             &m->dsinfo_elems,
	             dset_id,
	             mspace_id,
	             dspace_id,
	             glb_elems));

	TRY (h5priv_write_attrib (
	             m->mesh_gid,
	             "__num_elems__",
gsell's avatar
gsell committed
673
	             H5_INT64_T,
674
	             m->num_glb_elems,
gsell's avatar
gsell committed
675
	             m->num_leaf_levels));
676 677 678 679

	TRY (h5priv_write_attrib (
	             m->mesh_gid,
	             "__num_leaf_elems__",
gsell's avatar
gsell committed
680
	             H5_INT64_T,
681
	             m->num_glb_leaf_elems,
gsell's avatar
gsell committed
682
	             m->num_leaf_levels));
683 684 685 686

	TRY (h5priv_write_attrib (
	             m->mesh_gid,
	             "__num_leaf_levels__",
gsell's avatar
gsell committed
687
	             H5_INT16_T,
688 689 690 691 692 693
	             &m->num_leaf_levels,
	             1));

	TRY (h5priv_write_attrib (
	             m->mesh_gid,
	             "__is_chunked__",
gsell's avatar
gsell committed
694
	             H5_INT16_T,
695 696 697 698 699 700 701 702 703 704
	             &m->is_chunked,
	             1));

	// release mem
	TRY (h5_free (glb_elems));
	TRY (h5_free (chk_list));

	TRY (hdf5_close_dataspace (dspace_id));
	TRY (hdf5_close_dataspace (mspace_id));
	TRY (hdf5_close_dataset (dset_id));
gsell's avatar
gsell committed
705
	H5_RETURN (H5_SUCCESS);
706 707
}

gsell's avatar
gsell committed
708
static hid_t
709
set_chk_memspace (
gsell's avatar
gsell committed
710 711
	h5t_mesh_t* m,
	hid_t dataspace_id) {
gsell's avatar
gsell committed
712 713 714
	H5_PRIV_FUNC_ENTER (h5_err_t,
			    "m=%p, dataspace_id=%lld",
			    m, (long long int)dataspace_id);
715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734
	hid_t mspace_id;
	TRY (mspace_id = hdf5_create_dataspace (1, m->dsinfo_chunks.dims, NULL));

	hsize_t hstride = 1;
	hsize_t hstart = 0;
	hsize_t hcount = m->dsinfo_chunks.dims[0];
	if (m->f->myproc == 0) {
		TRY (hdf5_select_hyperslab_of_dataspace (
				        mspace_id,
				        H5S_SELECT_SET,
				        &hstart, &hstride, &hcount,
				        NULL));
	} else {
		hcount = 0;
		TRY (hdf5_select_hyperslab_of_dataspace (
						        mspace_id,
						        H5S_SELECT_SET,
						        &hstart, &hstride, &hcount,
						        NULL));
	}
gsell's avatar
gsell committed
735
	H5_RETURN (mspace_id);
736 737
}

gsell's avatar
gsell committed
738
static hid_t
739
set_chk_diskspace (
gsell's avatar
gsell committed
740 741 742
	h5t_mesh_t* m,
	hid_t dspace_id
	) {
gsell's avatar
gsell committed
743 744 745
	H5_PRIV_FUNC_ENTER (h5_err_t,
			    "m=%p, dataspace_id=%lld",
			    m, (long long int)dspace_id);
746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765

	H5Sset_extent_simple(dspace_id, 1,m->dsinfo_chunks.dims, NULL); //TODO WRITE WRAPPER

	hsize_t hstride = 1;
	hsize_t hstart = 0;
	hsize_t hcount = m->dsinfo_chunks.dims[0];
	if (m->f->myproc == 0) {
		TRY (hdf5_select_hyperslab_of_dataspace (
				        dspace_id,
				        H5S_SELECT_SET,
				        &hstart, &hstride, &hcount,
				        NULL));
	} else {
		hcount = 0;
		TRY (hdf5_select_hyperslab_of_dataspace (
						        dspace_id,
						        H5S_SELECT_SET,
						        &hstart, &hstride, &hcount,
						        NULL));
	}
gsell's avatar
gsell committed
766
	H5_RETURN (dspace_id);
767 768
}

gsell's avatar
gsell committed
769
static h5_err_t
770
write_chunks (
gsell's avatar
gsell committed
771
	h5t_mesh_t* const m
772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788
        ) {
	H5_PRIV_FUNC_ENTER (h5_err_t, "m=%p", m);
	m->dsinfo_chunks.dims[0] = m->chunks->num_alloc;


	TRY (h5priv_write_dataset_by_name (
			m,
			m->f,
			m->mesh_gid,
			&m->dsinfo_chunks,
			set_chk_memspace,
			set_chk_diskspace,
			m->chunks->chunks));

	TRY (h5priv_write_attrib (
	             m->mesh_gid,
	             "__num_chunks__",
789
	             H5_INT32_T,
790 791 792 793 794 795
	             &m->chunks->num_alloc,
	             1));

	TRY (h5priv_write_attrib (
	             m->mesh_gid,
	             "__num_chk_levels__",
796
	             H5_INT16_T, 	// note: better uint16?
797 798 799 800 801 802
	             &m->chunks->num_levels,
	             1));

	TRY (h5priv_write_attrib (
	             m->mesh_gid,
	             "__num_chk_p_level__",
803
	             H5_INT32_T,
804
	             m->chunks->num_chunks_p_level,
gsell's avatar
gsell committed
805
	             m->chunks->num_levels));
806

gsell's avatar
gsell committed
807
	H5_RETURN (H5_SUCCESS);
808 809
}

gsell's avatar
gsell committed
810
static hid_t
811
set_oct_memspace (
gsell's avatar
gsell committed
812 813 814
	h5t_mesh_t* m,
	hid_t dataspace_id
	) {
gsell's avatar
gsell committed
815 816 817
	H5_PRIV_FUNC_ENTER (h5_err_t,
			    "m=%p, dataspace_id=%lld",
			    m, (long long int)dataspace_id);
818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
	hid_t mspace_id;
	TRY (mspace_id = hdf5_create_dataspace (1, m->dsinfo_octree.dims, NULL));

	hsize_t hstride = 1;
	hsize_t hstart = 0;
	hsize_t hcount = m->dsinfo_octree.dims[0];
	if (m->f->myproc == 0) {
		TRY (hdf5_select_hyperslab_of_dataspace (
				        mspace_id,
				        H5S_SELECT_SET,
				        &hstart, &hstride, &hcount,
				        NULL));
	} else {
		hcount = 0;
		TRY (hdf5_select_hyperslab_of_dataspace (
						        mspace_id,
						        H5S_SELECT_SET,
						        &hstart, &hstride, &hcount,
						        NULL));
	}
gsell's avatar
gsell committed
838
	H5_RETURN (mspace_id);
839 840
}

gsell's avatar
gsell committed
841
static hid_t
842
set_oct_diskspace (
gsell's avatar
gsell committed
843 844 845
	h5t_mesh_t* m,
	hid_t dspace_id
	) {
gsell's avatar
gsell committed
846 847 848
	H5_PRIV_FUNC_ENTER (h5_err_t,
			    "m=%p, dataspace_id=%lld",
			    m, (long long int)dspace_id);
849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868

	H5Sset_extent_simple(dspace_id, 1,m->dsinfo_octree.dims, NULL); //TODO WRITE WRAPPER

	hsize_t hstride = 1;
	hsize_t hstart = 0;
	hsize_t hcount = m->dsinfo_octree.dims[0];
	if (m->f->myproc == 0) {
		TRY (hdf5_select_hyperslab_of_dataspace (
				        dspace_id,
				        H5S_SELECT_SET,
				        &hstart, &hstride, &hcount,
				        NULL));
	} else {
		hcount = 0;
		TRY (hdf5_select_hyperslab_of_dataspace (
						        dspace_id,
						        H5S_SELECT_SET,
						        &hstart, &hstride, &hcount,
						        NULL));
	}
gsell's avatar
gsell committed
869
	H5_RETURN (dspace_id);
870 871
}

gsell's avatar
gsell committed
872
static h5_err_t
873
write_octree (
gsell's avatar
gsell committed
874
	h5t_mesh_t* const m
875 876 877 878 879 880 881 882 883
        ) {
	H5_PRIV_FUNC_ENTER (h5_err_t, "m=%p", m);

	TRY (H5t_update_internal (m->octree));
	TRY (H5t_update_userdata (m->octree));

	m->dsinfo_octree.dims[0] = m->octree->current_oct_idx + 1;

	TRY (h5priv_write_dataset_by_name (
884 885 886 887 888 889 890
		     m,
		     m->f,
		     m->mesh_gid,
		     &m->dsinfo_octree,
		     set_oct_memspace,
		     set_oct_diskspace,
		     m->octree->octants));
891 892 893 894

	TRY (h5priv_write_attrib (
	             m->mesh_gid,
	             "__curr_oct_idx__",
895
	             H5_INT32_T,
896 897 898 899 900 901
	             &m->octree->current_oct_idx,
	             1));

	TRY (h5priv_write_attrib (
	             m->mesh_gid,
	             "__oct_maxpoints__",
902
	             H5_INT32_T,
903 904 905 906 907
	             &m->octree->maxpoints,
	             1));
	TRY (h5priv_write_attrib (
	             m->mesh_gid,
	             "__oct_size_userdata__",
908
	             H5_INT32_T,
909 910 911
	             &m->octree->size_userdata,
	             1));
	TRY (h5priv_write_attrib (
912 913 914 915
		     m->mesh_gid,
		     "__oct_bounding_box__",
		     H5_FLOAT64_T,
		     m->octree->bounding_box,
gsell's avatar
gsell committed
916
		     6));
917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932

	if (m->octree->size_userdata > 0) {
		m->dsinfo_userdata.dims[0] = m->octree->current_oct_idx + 1;

		TRY (h5priv_write_dataset_by_name (
					m,
					m->f,
					m->mesh_gid,
					&m->dsinfo_userdata,
					set_oct_memspace,    // should work fine same size as octree
					set_oct_diskspace,
					m->octree->userdata));


	}

gsell's avatar
gsell committed
933
	H5_RETURN (H5_SUCCESS);
934 935
}

gsell's avatar
gsell committed
936
static hid_t
937
set_weight_memspace (
gsell's avatar
gsell committed
938 939
	h5t_mesh_t* m,
	hid_t dataspace_id) {
gsell's avatar
gsell committed
940 941 942
	H5_PRIV_FUNC_ENTER (h5_err_t,
			    "m=%p, dataspace_id=%lld",
			    m, (long long int)dataspace_id);
943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962
	hid_t mspace_id;
	TRY (mspace_id = hdf5_create_dataspace (1, m->dsinfo_weights.dims, NULL));

	hsize_t hstride = 1;
	hsize_t hstart = 0;
	hsize_t hcount = m->dsinfo_weights.dims[0];
	if (m->f->myproc == 0) {
		TRY (hdf5_select_hyperslab_of_dataspace (
				        mspace_id,
				        H5S_SELECT_SET,
				        &hstart, &hstride, &hcount,
				        NULL));
	} else {
		hcount = 0;
		TRY (hdf5_select_hyperslab_of_dataspace (
						        mspace_id,
						        H5S_SELECT_SET,
						        &hstart, &hstride, &hcount,
						        NULL));
	}
gsell's avatar
gsell committed
963
	H5_RETURN (mspace_id);
964 965
}

gsell's avatar
gsell committed
966
static hid_t
967
set_weight_diskspace (
gsell's avatar
gsell committed
968 969 970
	h5t_mesh_t* m,
	hid_t dspace_id
	) {
gsell's avatar
gsell committed
971 972 973
	H5_PRIV_FUNC_ENTER (h5_err_t,
			    "m=%p, dataspace_id=%lld",
			    m, (long long int)dspace_id);
974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993

	H5Sset_extent_simple(dspace_id, 1,m->dsinfo_weights.dims, NULL); //TODO WRITE WRAPPER

	hsize_t hstride = 1;
	hsize_t hstart = 0;
	hsize_t hcount = m->dsinfo_weights.dims[0];
	if (m->f->myproc == 0) {
		TRY (hdf5_select_hyperslab_of_dataspace (
				        dspace_id,
				        H5S_SELECT_SET,
				        &hstart, &hstride, &hcount,
				        NULL));
	} else {
		hcount = 0;
		TRY (hdf5_select_hyperslab_of_dataspace (
						        dspace_id,
						        H5S_SELECT_SET,
						        &hstart, &hstride, &hcount,
						        NULL));
	}
gsell's avatar
gsell committed
994
	H5_RETURN (dspace_id);
995 996 997 998 999 1000
}

/*
 * weights is an array that stores the element weights. c weights per element with n elements, gives a size of c*n.
 * where the weights of an element are stored contiguosly. i.e. first weight of second elem is at weights[c*1].
 */
gsell's avatar
gsell committed
1001
static h5_err_t
1002
write_weights (
gsell's avatar
gsell committed
1003
	h5t_mesh_t* const m
1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
        ) {
	H5_PRIV_FUNC_ENTER (h5_err_t, "m=%p", m);

	m->dsinfo_weights.dims[0] = m->num_glb_elems[m->leaf_level] * m->num_weights;

	TRY (h5priv_write_dataset_by_name (
			m,
			m->f,
			m->mesh_gid,
			&m->dsinfo_weights,
			set_weight_memspace,
			set_weight_diskspace,
			m->weights));

	TRY (h5priv_write_attrib (
	             m->mesh_gid,
	             "__num_weights__",
1021
	             H5_INT32_T,
1022 1023 1024
	             &m->num_weights,
	             1));

gsell's avatar
gsell committed
1025
	H5_RETURN (H5_SUCCESS);
1026 1027
}
#endif
gsell's avatar
gsell committed
1028

1029 1030 1031 1032 1033 1034 1035
h5_err_t
h5tpriv_write_mesh (
        h5t_mesh_t* const m
        ) {
	H5_PRIV_API_ENTER (h5_err_t, "m=%p", m);
	if (m->mesh_changed) {
		if (m->is_chunked) {
gsell's avatar
gsell committed
1036
#ifdef WITH_PARALLEL_H5GRID
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
			if (m->num_weights > 0) {
				TRY (write_weights (m));
			}
			TRY (h5priv_mpi_barrier (m->f->props->comm));
			m->timing.measure[m->timing.next_time++] = MPI_Wtime();
			TRY (write_chunks (m));
			TRY (h5priv_mpi_barrier (m->f->props->comm));
			m->timing.measure[m->timing.next_time++] = MPI_Wtime();
			TRY (write_octree (m));
			TRY (h5priv_mpi_barrier (m->f->props->comm));
			m->timing.measure[m->timing.next_time++] = MPI_Wtime();
			if (m->f->nprocs > 1) {
				TRY (write_vertices_chk (m));
				TRY (h5priv_mpi_barrier (m->f->props->comm));
				m->timing.measure[m->timing.next_time++] = MPI_Wtime();
				TRY (write_elems_chk (m));
				TRY (h5priv_mpi_barrier (m->f->props->comm));
				m->timing.measure[m->timing.next_time++] = MPI_Wtime();
			} else {
				TRY (write_vertices (m));
				TRY (h5priv_mpi_barrier (m->f->props->comm));
				m->timing.measure[m->timing.next_time++] = MPI_Wtime();
				TRY (write_elems (m));
				TRY (h5priv_mpi_barrier (m->f->props->comm));
				m->timing.measure[m->timing.next_time++] = MPI_Wtime();
			}
#endif			
		} else {
			TRY (write_vertices (m));
			TRY (write_elems (m));
		}



	}
gsell's avatar
gsell committed
1072
	H5_RETURN (H5_SUCCESS);
1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085
}
// ANCHOR WRITE
/*
   Read vertices from file. If map is NULL, read *all* vertices otherwise the
   vertices specified in the map.
 */
static h5_err_t
read_vertices (
        h5t_mesh_t* m,
        h5_idxmap_t* map
        ) {
	H5_PRIV_FUNC_ENTER (h5_err_t, "m=%p", m);
	hid_t dset_id;
1086
	TRY (dset_id = hdf5_open_dataset_by_name (m->mesh_gid, m->dsinfo_vertices.name));
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
	hid_t mspace_id = H5S_ALL;
	hid_t dspace_id = H5S_ALL;

	if (map) {
		m->num_loc_vertices[m->num_leaf_levels-1] = map->num_items;
		m->last_stored_vid = m->num_loc_vertices[m->num_leaf_levels-1] - 1;
		TRY (h5tpriv_alloc_loc_vertices (m, map->num_items));

		// create memspace
		hsize_t num_loc_vertices = map->num_items;
		TRY (mspace_id = hdf5_create_dataspace(1, &num_loc_vertices, NULL));

		// create diskspace and select subset
		hsize_t num_glb_vertices = m->num_glb_vertices[m->num_leaf_levels-1];
		TRY (dspace_id = hdf5_create_dataspace (1, &num_glb_vertices, NULL));
		hsize_t hstride = 1;
		H5S_seloper_t seloper = H5S_SELECT_SET; // first selection
		for (hsize_t i = 0; i < map->num_items; i++) {
			hsize_t hstart = map->items[i].glb_idx;
			hsize_t hcount = 1;
			while (map->items[i].glb_idx+1 == map->items[i+1].glb_idx &&
			       i + 1 < map->num_items) {// WARNING check if +1 is correct here
				i++; hcount++;
			}
			TRY (hdf5_select_hyperslab_of_dataspace (
			             dspace_id,
			             seloper,
			             &hstart, &hstride, &hcount,
			             NULL));
			seloper = H5S_SELECT_OR;
		}
	} else {
		size_t num_vertices =  m->num_glb_vertices[m->num_leaf_levels-1];
		m->last_stored_vid = m->num_glb_vertices[m->num_leaf_levels-1] - 1;
		TRY (h5tpriv_alloc_loc_vertices (m, num_vertices));
	}

	TRY (h5priv_start_throttle (m->f));
	TRY (hdf5_read_dataset (
	             dset_id,
	             m->dsinfo_vertices.type_id,
	             mspace_id,
	             dspace_id,
	             m->f->props->xfer_prop,
	             m->vertices));
	TRY (h5priv_end_throttle (m->f));
	TRY (hdf5_close_dataspace (dspace_id));
	TRY (hdf5_close_dataspace (mspace_id));
	TRY (hdf5_close_dataset (dset_id));

gsell's avatar
gsell committed
1137
	H5_RETURN (H5_SUCCESS);
1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150
}

static h5_err_t
read_elems (
        h5t_mesh_t* const m,
        h5_loc_idx_t start,
        h5_loc_idx_t count,
        h5_glb_elem_t* glb_elems
        ) {
	H5_PRIV_FUNC_ENTER (h5_err_t, "m=%p, start=%lld, count=%lld",
	                    m, (long long)start, (long long)count);

	hid_t dset_id;
1151
	TRY (dset_id = hdf5_open_dataset_by_name (m->mesh_gid, m->dsinfo_elems.name));
1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180
	hid_t mspace_id;
	hsize_t hcount = (hsize_t)count;
	TRY (mspace_id = hdf5_create_dataspace (1, &hcount, NULL));

	hid_t dspace_id;
	hsize_t hstart = (hsize_t)start;
	hsize_t hstride = 1;
	hsize_t num_glb_elems = m->num_glb_elems[m->num_leaf_levels-1];
	TRY (dspace_id = hdf5_create_dataspace (1, &num_glb_elems, NULL));
	TRY (hdf5_select_hyperslab_of_dataspace (
	             dspace_id,
	             H5S_SELECT_SET,
	             &hstart, &hstride, &hcount,
	             NULL));

	TRY (h5priv_start_throttle (m->f));
	TRY (hdf5_read_dataset (
	             dset_id,
	             m->dsinfo_elems.type_id,
	             mspace_id,
	             dspace_id,
	             m->f->props->xfer_prop,
	             glb_elems));
	TRY (h5priv_end_throttle (m->f));

	TRY (hdf5_close_dataspace (dspace_id));
	TRY (hdf5_close_dataspace (mspace_id));
	TRY (hdf5_close_dataset (dset_id));

gsell's avatar
gsell committed
1181
	H5_RETURN (H5_SUCCESS);
1182 1183 1184 1185
}



gsell's avatar
gsell committed
1186
#if defined(WITH_PARALLEL_H5GRID)
1187 1188 1189 1190 1191 1192 1193 1194 1195
// READ MESH PARALLEL
/*
   Partition mesh via dual graph partitioning

   Step 1: Partiton dual graph of mesh, all procs have their global
          cell data loaded.
   Step 2: Handle ghost- and border cells
   Step 3: Read vertices
 */
gsell's avatar
gsell committed
1196
static h5_err_t
1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293
part_kway  (
        h5t_mesh_t* const m,
        h5_glb_elem_t** glb_elems
        ) {
	H5_PRIV_FUNC_ENTER (h5_err_t, "m=%p", m);
	/*
	   Step 1:
	   - read num cell/num procs cells
	   - call partitioner
	   - each proc must know the number of cells assigned to his
	    partition on foreign procs.
	   - send global cell data to right proc
	 */

	// compute initial distribution of cells on all procs
	int n = m->num_glb_elems[0] / m->f->nprocs;
	int r = m->num_glb_elems[0] % m->f->nprocs;

	idx_t* vtxdist;
	TRY (vtxdist = h5_calloc (m->f->nprocs+1, sizeof (*vtxdist)));
	vtxdist[0] = 0;
	h5_debug ("vtxdist[%d]: %d", 0, 0);
	for (int i = 1; i < m->f->nprocs+1; i++) {
		if (r > 0) {
			vtxdist[i] = vtxdist[i-1] + n + 1;
			r--;
		} else {
			vtxdist[i] = vtxdist[i-1] + n;
		}
		h5_debug ("vtxdist[%d]: %d", i, vtxdist[i]);
	}
	// read cells only
	idx_t start = vtxdist[m->f->myproc];
	idx_t num_interior_elems = vtxdist[m->f->myproc+1] - start;
	m->num_interior_elems[0] = m->num_interior_leaf_elems[0] = num_interior_elems;

	h5_glb_elem_t* elems;
	TRY (elems = h5tpriv_alloc_glb_elems (m, num_interior_elems));

	TRY (read_elems (m, start, num_interior_elems, elems));


	// setup input for ParMETIS
	idx_t* xadj;
	idx_t* adjncy;
	idx_t* part;
	TRY (xadj = h5_calloc (num_interior_elems, sizeof(*xadj)));
	TRY (part = h5_calloc (num_interior_elems, sizeof(*part)));
	//  TODO: 4*num_interior_elems will work for meshes with up to 4 facets only!
	TRY (adjncy = h5_calloc (4*num_interior_elems, sizeof(*adjncy)));
	idx_t i, j;
	int num_facets = h5tpriv_ref_elem_get_num_facets (m);
	for (i = 0, j = 0; i < num_interior_elems; i++) {
		h5_glb_idx_t* neighbors = h5tpriv_get_glb_elem_neighbors(m, elems, i);
		xadj[i] = j;
		h5_debug ("xadj[%d]: %d", i, j);
		// for all facets
		for (int l = 0; l < num_facets; l++) {
			if (neighbors[l] < 0) continue;
			adjncy[j] = neighbors[l];
			h5_debug ("adjncy[%d]: %d", j, adjncy[j]);
			j++;
		}
	}
	xadj[num_interior_elems] = j;
	h5_debug ("xadj[%lld]: %d", (long long)num_interior_elems, j);
	// now we can call the partitioner
	idx_t wgtflag = 0;
	idx_t numflag = 0;
	idx_t ncon = 1;
	idx_t nparts = m->f->nprocs;
	real_t* tpwgts;
	real_t* ubvec;
	idx_t options[] = {1,127,42};
	idx_t edgecut;
	h5_debug ("nparts: %d", nparts);
	TRY (tpwgts = h5_calloc (nparts, sizeof(*tpwgts)));
	TRY (ubvec = h5_calloc (nparts, sizeof(*ubvec)));
	for (i = 0; i < nparts; i++) {
		tpwgts[i] = 1.0 / (real_t)nparts;
		ubvec[i] = 1.05;
	}
	int rc = ParMETIS_V3_PartKway (
	        vtxdist,
	        xadj,
	        adjncy,
	        NULL,           // vwgt
	        NULL,           // adjwgt
	        &wgtflag,
	        &numflag,
	        &ncon,
	        &nparts,
	        tpwgts,
	        ubvec,
	        options,
	        &edgecut,
	        part,
gsell's avatar
gsell committed
1294
	        &m->f->props->comm
1295 1296
	        );
	if (rc != METIS_OK) {
1297 1298 1299
		H5_RETURN_ERROR (
		        H5_ERR,
			"ParMETIS failed");
1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332
	}
	TRY (h5_free (vtxdist));
	TRY (h5_free (xadj));
	TRY (h5_free (adjncy));
	TRY (h5_free (tpwgts));
	TRY (h5_free (ubvec));

#if !defined(NDEBUG)
	for (i = 0; i < num_interior_elems; i++) {
		h5_debug ("part[%d]: %llu", i, (unsigned long long)part[i]);
	}
#endif
	/*
	   Now we know the partitioning, but the cells aren't distributed
	   accordingly.

	   Eeach processor knows the number of cells he has to send the other
	   procs in the group, but still don't how many cells he will receive
	   from the other procs in the group.
	 */

	// So, let's  count the number of cells we have to send to each proc.
	int* sendcounts;
	int* recvcounts;
	TRY (sendcounts = h5_calloc (nparts, sizeof(sendcounts[0])));
	TRY (recvcounts = h5_calloc (nparts, sizeof(recvcounts[0])));
	for (i = 0; i < num_interior_elems; i++) {
		sendcounts[part[i]]++;
	}
	// send these numbers to according procs
	TRY (h5priv_mpi_alltoall (
	             sendcounts, 1, MPI_INT,
	             recvcounts, 1, MPI_INT,
gsell's avatar
gsell committed
1333
	             m->f->props->comm));
1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387

	for (i = 0; i < nparts; i++) {
		h5_debug ("sendcounts[%d]: %d", i, sendcounts[i]);
		h5_debug ("recvcounts[%d]: %d", i, recvcounts[i]);
	}
	/*
	   next step is to scatter the cells to their procs
	   via an all-to-all communication:
	   - allocate and setup send buffer
	   - allocate and setup recv buffer
	   - setup MPI data type
	   - scatter all-to-all
	 */

	// allocate and initialize send buffer num_interior_elems*sizeof(cell)
	h5_glb_elem_t* sendbuf;
	TRY (sendbuf = h5tpriv_alloc_glb_elems (m, num_interior_elems));
	int* senddispls;
	TRY (senddispls = h5_calloc (m->f->nprocs, sizeof (senddispls[0])));

	senddispls[0] = 0;
	for (i = 0; i < m->f->nprocs-1; i++) {
		senddispls[i+1] = senddispls[i] + sendcounts[i];
		sendcounts[i] = 0;
	}
	sendcounts[i] = 0;
	for (i = 0; i < num_interior_elems; i++) {
		size_t sendidx = senddispls[part[i]] + sendcounts[part[i]];
		h5tpriv_copy_glb_elems (
		        m,
		        sendbuf, sendidx,
		        elems, i, 1);
		sendcounts[part[i]]++;
	}
	for (i = 0; i < num_interior_elems; i++) {
		h5_debug ("sendbuf[%d]: %lld", i, h5tpriv_get_glb_elem_idx (m, sendbuf, i));
	}
	TRY (h5_free (part));

	// allocate and initialize recv buffer
	int* recvdispls;
	recvdispls = h5_calloc (m->f->nprocs, sizeof (recvdispls[0]));
	recvdispls[0] = 0;
	for (i = 0; i < m->f->nprocs-1; i++) {
		recvdispls[i+1] = recvdispls[i] + recvcounts[i];
	}
	num_interior_elems = recvdispls[i] + recvcounts[i];
	h5_glb_elem_t* recvbuf;
	TRY (recvbuf = h5tpriv_alloc_glb_elems (m, num_interior_elems));
	// scatter elems all to all
	MPI_Datatype type = h5tpriv_get_mpi_type_of_glb_elem (m);
	TRY (h5priv_mpi_alltoallv (
	             sendbuf, sendcounts, senddispls, type,
	             recvbuf, recvcounts, recvdispls, type,
gsell's avatar
gsell committed
1388
	             m->f->props->comm));
1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401
	for (i = 0; i < num_interior_elems; i++) {
		h5_debug ("global cell ID[%d]: %lld",
		          i, h5tpriv_get_glb_elem_idx (m, recvbuf, i));
	}
	TRY (h5_free (sendbuf));
	TRY (h5_free (sendcounts));
	TRY (h5_free (senddispls));
	TRY (h5_free (recvcounts));
	TRY (h5_free (recvdispls));

	TRY (h5_free (elems));
	*glb_elems = recvbuf;

gsell's avatar
gsell committed
1402
	H5_RETURN (H5_SUCCESS);
1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416
}


/*
   exchange ghost cells:
   idea: send my ghost cells to all procs
   1. compute ghost cells
   2. allgather ghost cell IDs: each proc knows the ghost cell IDs of all procs
   3. compute number of border cells to scatter to each proc
   4. scatter these numbers
   5. build array with (border)cells to scatter (note: we may have to scatter
     the same cell to multiple procs)
   6. scatter border cells with alltoallv
 */
gsell's avatar
gsell committed
1417
static h5_err_t
1418 1419 1420 1421 1422 1423
exchange_ghost_cells (
        h5t_mesh_t* const m,
        h5_glb_elem_t* glb_elems,
        h5_glb_elem_t** ghost_elems,
        size_t* num_ghost_elems
        ) {
gsell's avatar
gsell committed
1424
	H5_PRIV_FUNC_ENTER (h5_err_t, "m=%p, ghost_elems=%p", m, ghost_elems);
1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457
	int* sendcounts;
	int* senddispls;
	int* recvcounts;
	int* recvdispls;
	int nprocs = m->f->nprocs;
	TRY (sendcounts = h5_calloc (nprocs, sizeof(*sendcounts)));
	TRY (senddispls = h5_calloc (nprocs, sizeof(*senddispls)));
	TRY (recvcounts = h5_calloc (nprocs, sizeof(*recvcounts)));
	TRY (recvdispls = h5_calloc (nprocs, sizeof(*recvdispls)));

	// determine my ghost cells
	h5_glb_idxlist_t* loc_ghostcell_ids = NULL;
	int num_facets = h5tpriv_ref_elem_get_num_facets (m);
	for (int i = 0; i < m->num_interior_elems[0]; i++) {
		h5_glb_idx_t* neighbors = h5tpriv_get_glb_elem_neighbors(m, glb_elems, i);
		for (int facet = 0; facet < num_facets; facet++) {
			if (neighbors[facet] == -1) {
				// geometric boundary
				continue;
			}
			if (h5priv_search_idxmap (&m->map_elem_g2l, neighbors[facet]) >= 0) {
				// neighbor is local
				continue;
			}
			// neighbor is *not* local
			TRY (h5priv_search_in_glb_idxlist (&loc_ghostcell_ids, neighbors[facet]));
			h5_debug ("ghost cell: %lld", neighbors[facet]);
		}
	}
	// allgather number of ghost cells
	int* num_ghostcells;
	TRY (num_ghostcells = h5_calloc (nprocs, sizeof(num_ghostcells[0])));
	TRY (mpi_allgather (&loc_ghostcell_ids->num_items, 1, MPI_INT,
gsell's avatar
gsell committed
1458
	                    num_ghostcells, 1, MPI_INT, m->f->props->comm));
1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476
	for (int i = 0; i < nprocs; i++) {
		h5_debug ("num_ghostcells[%d] = %d", i, num_ghostcells[i]);
	}

	// allgather ghost cell IDs
	int num_ghostcells_total = num_ghostcells[0];
	recvdispls[0] = 0;
	for (int i = 1; i < nprocs; i++) {
		recvdispls[i] = num_ghostcells_total;
		num_ghostcells_total += num_ghostcells[i];
	}
	h5_glb_id_t* ghostcells;
	TRY (ghostcells = h5_calloc (num_ghostcells_total, sizeof(*ghostcells)));
	TRY (mpi_allgatherv (
	             loc_ghostcell_ids->items, (int)loc_ghostcell_ids->num_items,
	             MPI_LONG_LONG,
	             ghostcells, num_ghostcells, recvdispls,
	             MPI_LONG_LONG,
gsell's avatar
gsell committed
1477
	             m->f->props->comm));
1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536

	for (int i = 0; i < num_ghostcells_total; i++) {
		h5_debug ("ghostcells[%d] = %lld", i, ghostcells[i]);
	}

	// scatter my border cells
	// - count and collect border cells we have to scatter
	h5_loc_idxlist_t** belem_lists;
	TRY (belem_lists = h5_calloc (nprocs, sizeof (*belem_lists)));
	h5_loc_idx_t num_elems_to_scatter_total = 0;
	for (int proc = 0; proc < nprocs; proc++) {
		h5_loc_idxlist_t** list = &belem_lists[proc];
		h5_loc_idx_t last = recvdispls[proc] + num_ghostcells[proc] - 1;
		for (int i = recvdispls[proc]; i <= last; i++) {
			// is cell with ID local?
			h5_loc_idx_t idx;
			idx = h5priv_search_idxmap (&m->map_elem_g2l, ghostcells[i]);
			if (idx >= 0) {
				// yes: we have to send this cell to proc
				// add to collection
				TRY (h5priv_insert_into_loc_idxlist (list, idx, -1));
				num_elems_to_scatter_total++;
			}
		}
	}
	TRY (h5_free (num_ghostcells));
	TRY (h5_free (ghostcells));

	// - allocate memory for border cells we have to scatter
	h5_glb_elem_t* sendbuf;
	TRY (sendbuf = h5tpriv_alloc_glb_elems (m, num_elems_to_scatter_total));

	// - setup send buffer
	for (int sendidx = 0, proc = 0; proc < nprocs; proc++) {
		h5_loc_idxlist_t* list = belem_lists[proc];
		if (list) {
			sendcounts[proc] = list->num_items;
			senddispls[proc] = sendidx;
			for (size_t i = 0; i < list->num_items; i++, sendidx++) {
				// copy glb cell at list->item[i] to sendbuf
				h5tpriv_copy_glb_elems (
				        m,
				        sendbuf, sendidx,
				        glb_elems,
				        list->items[i],
				        1);
			}
			TRY (h5_free (list));
		} else { // nothing to to for this proc
			sendcounts[proc] = 0;
			senddispls[proc] = sendidx;
		}
	}
	TRY (h5_free (belem_lists));

	// - scatter send counts
	TRY (h5priv_mpi_alltoall (
	             sendcounts, 1, MPI_INT,
	             recvcounts, 1, MPI_INT,
gsell's avatar
gsell committed
1537
	             m->f->props->comm));
1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553

	// compute receive displacements and number of local ghost cells
	recvdispls[0] = 0;
	for (int proc = 0; proc < nprocs-1; proc++) {
		recvdispls[proc+1] = recvdispls[proc] + recvcounts[proc];
	}
	int num_loc_ghost_elems = recvdispls[nprocs-1] + recvcounts[nprocs-1];

	// - scatter ghost cells alltoall
	h5_glb_elem_t* recvbuf;
	TRY (recvbuf = h5tpriv_alloc_glb_elems (m, num_loc_ghost_elems));
	MPI_Datatype type = h5tpriv_get_mpi_type_of_glb_elem (m);

	TRY (h5priv_mpi_alltoallv (
	             sendbuf, sendcounts, senddispls, type,
	             recvbuf, recvcounts, recvdispls, type,
gsell's avatar
gsell committed
1554
	             m->f->props->comm));
1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568

	for (int i = 0; i < num_loc_ghost_elems; i++) {
		h5_debug ("global ghost cell ID[%d]: %lld",
		          i, h5tpriv_get_glb_elem_idx (m, recvbuf, i));
	}
	TRY (h5_free (sendbuf));

	TRY (h5_free (sendcounts));
	TRY (h5_free (senddispls));
	TRY (h5_free (recvcounts));
	TRY (h5_free (recvdispls));

	*ghost_elems = recvbuf;
	*num_ghost_elems = num_loc_ghost_elems;
gsell's avatar
gsell committed
1569
	H5_RETURN (H5_SUCCESS);
1570 1571 1572 1573 1574 1575 1576 1577
}


h5_err_t
h5tpriv_read_mesh (
        h5t_mesh_t* const m
        ) {
	H5_PRIV_API_ENTER (h5_err_t, "m=%p", m);
gsell's avatar
gsell committed
1578
	h5_glb_elem_t* glb_elems = NULL;
1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647
	TRY (part_kway (m, &glb_elems));
	h5_loc_idx_t num_interior_elems = m->num_interior_elems[0];

	// add interior elements to global -> local index map
	TRY (h5tpriv_init_map_elem_g2l (m, glb_elems, num_interior_elems));

	// gather ghost cells
	h5_glb_elem_t* ghost_elems;
	size_t num_ghost_elems;
	TRY (exchange_ghost_cells (m, glb_elems, &ghost_elems, &num_ghost_elems));
	m->num_ghost_elems[0] = num_ghost_elems;

	// add ghost cells to global -> local index map
	TRY (h5tpriv_init_map_elem_g2l (m, ghost_elems, num_ghost_elems));

	// define local indices for all vertices of all local elements
	size_t size = num_interior_elems+num_ghost_elems;
	TRY (h5priv_new_idxmap (&m->map_vertex_g2l, size+128));

	h5_idxmap_t* map = &m->map_vertex_g2l;

	/* for the time being we use the hash table only for a fast test
	   whether a global index has already been added to the map or not. */
	h5_hashtable_t htab;
	TRY (h5priv_hcreate ((size << 2) / 3, &htab,
	                     hidxmap_cmp, hidxmap_compute_hval, NULL));

	int num_vertices = h5tpriv_ref_elem_get_num_vertices (m);
	for (size_t idx = 0; idx < num_interior_elems; idx++) {
		h5_glb_idx_t* vertices = h5tpriv_get_glb_elem_vertices (m, glb_elems, idx);
		for (int i = 0; i < num_vertices; i++) {
			// add index temporarly to map ...
			map->items[map->num_items] = (h5_idxmap_el_t) {vertices[i], 0};
			// ... and check whether it has already been added
			h5_idxmap_el_t* retval;
			h5priv_hsearch (&map->items[map->num_items],
			                H5_ENTER, (void**)&retval, &htab);
			if (retval == &map->items[map->num_items]) {
				// new entry in hash table thus in map
				map->num_items++;
			}
		}
	}
	// same for ghost cells
	for (size_t idx = 0; idx < num_ghost_elems; idx++) {
		h5_glb_idx_t* vertices = h5tpriv_get_glb_elem_vertices (m, ghost_elems, idx);
		for (int i = 0; i < num_vertices; i++) {
			// add index temporarly to map ...
			map->items[map->num_items] = (h5_idxmap_el_t) {vertices[i], 0};
			// ... and check whether it has already been added
			h5_idxmap_el_t* retval;
			h5priv_hsearch (&map->items[map->num_items],
			                H5_ENTER, (void**)&retval, &htab);
			if (retval == &map->items[map->num_items]) {
				// new entry in hash table thus in map
				map->num_items++;
			}
		}
	}
	TRY (h5priv_hdestroy (&htab));
	h5priv_sort_idxmap (map);
	for (h5_loc_idx_t i = 0; i < map->num_items; i++) {
		map->items[i].loc_idx = i;
	}
	TRY (read_vertices (m, map));

	TRY (h5tpriv_alloc_loc_elems (m, 0, num_interior_elems+num_ghost_elems));
	m->num_loaded_levels = 1;

gsell's avatar
gsell committed
1648
	TRY (h5tpriv_init_loc_elems_struct (m, glb_elems, 0, num_interior_elems, 0, NULL));
gsell's avatar
gsell committed
1649
	TRY (h5tpriv_init_loc_elems_struct (
gsell's avatar
gsell committed
1650
		     m, ghost_elems, num_interior_elems, num_ghost_elems, H5_GHOST_ENTITY, NULL));
1651 1652 1653 1654 1655 1656 1657

	TRY (h5_free (glb_elems));
	TRY (h5_free (ghost_elems));

	TRY (h5tpriv_init_elem_flags (m, 0, num_interior_elems+num_ghost_elems));

	TRY (h5tpriv_update_internal_structs (m, 0));
gsell's avatar
gsell committed
1658
	H5_RETURN (H5_SUCCESS);
1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690
}
#else

// THE SERIAL VERSION ...
h5_err_t
h5tpriv_read_mesh (
        h5t_mesh_t* const m
        ) {
	H5_PRIV_API_ENTER (h5_err_t, "m=%p", m);
	// local and global counts are identical in serial case
	for (size_t lvl = 0; lvl < m->num_leaf_levels; lvl++) {
		m->num_loc_vertices[lvl] = m->num_glb_vertices[lvl];
		m->num_interior_elems[lvl] = m->num_glb_elems[lvl];
		m->num_interior_leaf_elems[lvl] = m->num_glb_leaf_elems[lvl];
	}

	TRY (read_vertices (m, NULL));
	TRY (h5tpriv_rebuild_map_vertex_g2l (m, 0, m->num_leaf_levels-1));

	h5_loc_idx_t num_interior_elems = m->num_interior_elems[m->num_leaf_levels-1];
	h5_glb_elem_t* glb_elems;
	TRY (glb_elems = h5tpriv_alloc_glb_elems (m, num_interior_elems));
	TRY (read_elems (m, 0, num_interior_elems, glb_elems));
	TRY (h5tpriv_alloc_loc_elems (m, 0, num_interior_elems));
	m->num_loaded_levels = m->num_leaf_levels;

	TRY (h5tpriv_init_map_elem_g2l (m, glb_elems, num_interior_elems));
	TRY (h5tpriv_init_loc_elems_struct (m, glb_elems, 0, num_interior_elems, 0, NULL));
	TRY (h5tpriv_update_internal_structs (m, 0));
	TRY (h5tpriv_init_elem_flags (m, 0, num_interior_elems));
	TRY (h5_free (glb_elems));

gsell's avatar
gsell committed
1691
	H5_RETURN (H5_SUCCESS);
1692 1693
}
#endif
gsell's avatar
gsell committed
1694 1695 1696


#ifdef WITH_PARALLEL_H5GRID
1697 1698 1699 1700 1701 1702
static h5_err_t
read_octree (
        h5t_mesh_t* m
        ) {
	H5_PRIV_FUNC_ENTER (h5_err_t, "m=%p", m);
	hid_t dset_id, dset_id2;
1703
	TRY (dset_id = hdf5_open_dataset_by_name (m->mesh_gid, m->dsinfo_octree.name));
1704 1705 1706 1707 1708 1709 1710 1711 1712
	hid_t mspace_id = H5S_ALL;
	hid_t dspace_id = H5S_ALL;


	int oct_size = -1;
	int maxpoints = -1;
	int size_userdata = -1;
	h5_float64_t bounding_box[6];
	TRY (h5priv_read_attrib (
1713 1714 1715 1716
		     m->mesh_gid,
		     "__curr_oct_idx__",
		     H5_INT32_T,
		     &oct_size));
1717
	TRY (h5priv_read_attrib (
1718 1719 1720 1721
		     m->mesh_gid,
		     "__oct_maxpoints__",
		     H5_INT32_T,
		     &maxpoints));
1722
	TRY (h5priv_read_attrib (
1723 1724 1725 1726
		     m->mesh_gid,
		     "__oct_size_userdata__",
		     H5_INT32_T,
		     &size_userdata));
1727
	TRY (h5priv_read_attrib (
1728 1729 1730 1731
		     m->mesh_gid,
		     "__oct_bounding_box__",
		     H5_FLOAT64_T,
		     bounding_box))
1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753
	h5t_octant_t*   octants;
	h5t_oct_userdata_t*  userdata;
	TRY (H5t_read_octree (
                     &m->octree,
                     oct_size,
                     size_userdata,
                     maxpoints,
                     &octants, 
                     (void **) &userdata,
                     m->f->props->comm));
	TRY (H5t_set_bounding_box (m->octree, bounding_box));
	TRY (h5priv_start_throttle (m->f));

	TRY (hdf5_read_dataset (
             dset_id,
             m->dsinfo_octree.type_id,
             mspace_id,
             dspace_id,
             m->f->props->xfer_prop,
             octants));

	if (size_userdata > 0) {
1754 1755
		TRY (dset_id2 = hdf5_open_dataset_by_name (
			     m->mesh_gid, m->dsinfo_userdata.name));
1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773
		TRY (hdf5_read_dataset (
	             dset_id2,
	             m->dsinfo_userdata.type_id,
	             mspace_id,
	             dspace_id,
	             m->f->props->xfer_prop,
	             userdata));
		TRY (hdf5_close_dataset (dset_id2));
	}
	TRY (h5priv_end_throttle (m->f));


	TRY (hdf5_close_dataspace (dspace_id));
	TRY (hdf5_close_dataspace (mspace_id));
	TRY (hdf5_close_dataset (dset_id));



gsell's avatar
gsell committed
1774
	H5_RETURN (H5_SUCCESS);
1775 1776 1777
}
static h5_err_t
read_weights (
gsell's avatar
gsell committed
1778 1779
	h5t_mesh_t* m
	) {
1780 1781
	H5_PRIV_FUNC_ENTER (h5_err_t, "m=%p", m);
	hid_t dset_id;
1782
	TRY (dset_id = hdf5_open_dataset_by_name (m->mesh_gid, m->dsinfo_weights.name));
1783 1784 1785 1786 1787
	hid_t mspace_id = H5S_ALL;
	hid_t dspace_id = H5S_ALL;


	TRY (h5priv_read_attrib (
1788 1789 1790 1791
		     m->mesh_gid,
		     "__num_weights__",
		     H5_INT32_T,
		     &m->num_weights));
gsell's avatar
gsell committed
1792 1793
	TRY (m->weights =
	     h5_calloc (m->num_weights * m->num_glb_elems[m->num_leaf_levels-1], sizeof (*m->weights)));
1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810
	if (m->num_weights < 1) {
		m->weights = NULL;
	}
	TRY (h5priv_start_throttle (m->f));

	TRY (hdf5_read_dataset (
		             dset_id,
		             m->dsinfo_weights.type_id,
		             mspace_id,
		             dspace_id,
		             m->f->props->xfer_prop,
		             m->weights));
	TRY (h5priv_end_throttle (m->f));

	// check that weights are > 0
	for (h5_glb_idx_t i = 0; i < m->num_weights * m->num_glb_elems[m->num_leaf_levels-1]; i++) {
		if (m->weights[i] < 1) {
gsell's avatar
gsell committed
1811 1812 1813 1814
			h5_debug ("Warning: weight %d from elem %lld was %d ",
				  (int) i%m->num_weights,
				  (long long int) i/m->num_weights,
				  m->weights[i]);
1815 1816 1817 1818 1819 1820 1821 1822
			m->weights[i] = 1;

		}
	}

	TRY (hdf5_close_dataspace (dspace_id));
	TRY (hdf5_close_dataspace (mspace_id));
	TRY (hdf5_close_dataset (dset_id));
gsell's avatar
gsell committed
1823
	H5_RETURN (H5_SUCCESS);
1824 1825 1826 1827 1828 1829 1830 1831
}

static h5_err_t
read_chunks (
        h5t_mesh_t* m
        ) {
	H5_PRIV_FUNC_ENTER (h5_err_t, "m=%p", m);
	hid_t dset_id;
1832
	TRY (dset_id = hdf5_open_dataset_by_name (m->mesh_gid, m->dsinfo_chunks.name));
1833 1834 1835 1836 1837
	hid_t mspace_id = H5S_ALL;
	hid_t dspace_id = H5S_ALL;

	TRY (m->chunks = h5_calloc (1, sizeof (*m->chunks)));
	TRY (h5priv_read_attrib (
1838 1839 1840 1841
		     m->mesh_gid,
		     "__num_chunks__",
		     H5_INT32_T,
		     &m->chunks->num_alloc));
1842 1843
	m->chunks->curr_idx = m->chunks->num_alloc -1;
	TRY (h5priv_read_attrib (
1844 1845 1846 1847
		     m->mesh_gid,
		     "__num_chk_levels__",
		     H5_INT16_T,
		     &m->chunks->num_levels));
1848

gsell's avatar
gsell committed
1849 1850
	TRY (m->chunks->num_chunks_p_level =
	     h5_calloc (m->chunks->num_levels, sizeof (*m->chunks->num_chunks_p_level)));
1851 1852

	TRY (h5priv_read_attrib (
1853 1854 1855 1856
		     m->mesh_gid,
		     "__num_chk_p_level__",
		     H5_INT32_T,
		     m->chunks->num_chunks_p_level));
gsell's avatar
gsell committed
1857 1858
	TRY (m->chunks->chunks =
	     h5_calloc (m->chunks->num_alloc, sizeof (*m->chunks->chunks)));
1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875

	TRY (h5priv_start_throttle (m->f));

		TRY (hdf5_read_dataset (
	             dset_id,
	             m->dsinfo_chunks.type_id,
	             mspace_id,
	             dspace_id,
	             m->f->props->xfer_prop,
	             m->chunks->chunks));
	TRY (h5priv_end_throttle (m->f));


	TRY (hdf5_close_dataspace (dspace_id));
	TRY (hdf5_close_dataspace (mspace_id));
	TRY (hdf5_close_dataset (dset_id));

gsell's avatar
gsell committed
1876
	H5_RETURN (H5_SUCCESS);
1877 1878 1879
}

/*
gsell's avatar
gsell committed
1880 1881 1882 1883
  get weights of octant (i.e. of all elements that belong to a chunk that
  belongs to the octant or its parents) for parent octants a factor is used.
  therefore the weight of an octant is divided equally onto it's children
*/
1884 1885 1886

static h5_err_t
get_weights_of_octant (
gsell's avatar
gsell committed
1887 1888 1889 1890 1891 1892
	h5t_mesh_t* const m,
	h5t_octree_t* octree,
	h5_oct_idx_t oct_idx,
	double factor,
	idx_t* weights
	) {
1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915
	H5_PRIV_FUNC_ENTER (h5_err_t, "m=%p, octree=%p, oct_idx=%d, factor=%4.4f, weights=%p",
			m, octree, oct_idx, factor, weights);

	h5t_oct_userdata_t* userdata = NULL;

	TRY (H5t_get_userdata_r (octree, oct_idx, (void**) &userdata));
	for (int i = 0; i < MAX_CHUNKS_PER_OCTANT; i++) { // iterate through all chunks in octant
		if (userdata->idx[i] != -1) {
			h5_chk_idx_t chk_idx = userdata->idx[i];
			h5_glb_idx_t first_elem = m->chunks->chunks[chk_idx].elem;
			h5_glb_idx_t num_elems = m->chunks->chunks[chk_idx].num_elems;
			// iterate through all elements in chunk
			for (h5_glb_idx_t j = first_elem; j < first_elem + num_elems; j++) {
				for (h5_weight_t k = 0; k < m->num_weights; k++) {
					weights[k] += (h5_weight_t) (m->weights[j * m->num_weights + k] * factor);
				}
			}

		}
	} // add weights of parents proportionally
	while ((oct_idx = H5t_get_parent (octree, oct_idx)) != -1) {