Exemplo n.º 1
0
/*-------------------------------------------------------------------------
 * Function:  allocate_and_init_2D_array
 *
 * Purpose:   Initialize an array as a pseudo 2D array and copy in some
 *            initial values.
 *
 * Return:    SUCCEED/FAIL
 *
 *-------------------------------------------------------------------------
 */
static herr_t
allocate_and_init_2D_array(int ***arr, const hsize_t *sizes, int **initial_values) {

    size_t r, c;        /* Data rows and columns    */
    size_t i;           /* Iterator                 */
    size_t n_bytes;     /* # of bytes to copy       */

    r = (size_t)sizes[0];
    c = (size_t)sizes[1];

    /* Allocate and set up pseudo-2D array */
    if (NULL == (*arr = (int **)HDcalloc(r, sizeof(int *))))
        TEST_ERROR;
    if (NULL == ((*arr)[0] = (int *)HDcalloc(r * c, sizeof(int))))
        TEST_ERROR;
    for (i = 0; i < r; i++)
        (*arr)[i] = (**arr + c * i);

    /* Copy over the data elements */
    if (initial_values) {
        n_bytes = r * c * sizeof(int);
        HDmemcpy((*arr)[0], initial_values[0], n_bytes);
    }

    return SUCCEED;
error:
    free_2D_array(arr);

    return FAIL;
} /* end allocate_and_init_2D_array() */
Exemplo n.º 2
0
/******************************************************************************
 NAME
     HULcreate_list - Create a linked list

 DESCRIPTION
    Creates a linked list.  The list may either be sorted or un-sorted, based
    on the comparison function.

 RETURNS
    Returns a pointer to the list if successful and NULL otherwise

*******************************************************************************/
list_head_t *HULcreate_list(HULfind_func_t find_func    /* IN: object comparison function */
)
{
    CONSTR(FUNC, "HULcreate_list");	/* for HERROR */
    list_head_t *ret_value=NULL;     /* ptr to the linked list "head" node */

    HEclear();

    /* Allocate the head information */
    if((ret_value=(list_head_t *)HDcalloc(1,sizeof(list_head_t)))==NULL)
        HGOTO_ERROR(DFE_NOSPACE, NULL);

    /* Set the counter */
    ret_value->count=0;

    /* Store the creation flags, etc */
    if(find_func==NULL)
        ret_value->flags=HUL_UNSORTED_LIST;
    else
        ret_value->flags=HUL_SORTED_LIST;
    ret_value->cmp_func=find_func;

done:
  if(ret_value == NULL)   
    { /* Error condition cleanup */

    } /* end if */

  /* Normal function cleanup */
  return ret_value;
}   /* end HULcreate_list() */
Exemplo n.º 3
0
static void
create_textfile(const char *name, size_t size)
{
    char *buf;
    int fd;
    size_t i;
    char *bp;
    ssize_t nbytes;

    fd = HDcreat(name,0777);
    HDassert(fd >= 0);
    buf = (char *)HDcalloc(size, (size_t)1);
    HDassert(buf);

    /* fill buf with pattern */
    bp = buf;
    for(i = 0; i < size; i++)
        *bp++ = pattern[i % 10];

    nbytes = HDwrite(fd, buf, size);
    HDassert(nbytes >= 0);

    HDfree(buf);

    HDclose(fd);
}
Exemplo n.º 4
0
/*-------------------------------------------------------------------------
 * Function:	H5MM_calloc
 *
 * Purpose:	Similar to the POSIX version of calloc(3), except this routine
 *              just takes a 'size' parameter. This routine
 *		specifically checks for allocations of 0 bytes and fails
 *              in that case.  This routine is not called when NDEBUG is
 *		defined.
 *
 * Return:	Success:	Ptr to new memory
 *
 *		Failure:	NULL
 *
 * Programmer:	Quincey Koziol
 *		[email protected]
 *		Nov  8 2003
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
void *
H5MM_calloc(size_t size)
{
    /* Use FUNC_ENTER_NOAPI_NOINIT_NOFUNC here to avoid performance issues */
    FUNC_ENTER_NOAPI_NOINIT_NOFUNC(H5MM_calloc);

    assert(size);

    FUNC_LEAVE_NOAPI(HDcalloc(1,size));
} /* end H5MM_calloc() */
Exemplo n.º 5
0
herr_t
H5AwriteVL_str
    (JNIEnv *env, hid_t aid, hid_t tid, jobjectArray buf)
{
    herr_t  status = -1;
    char  **wdata;
    jsize   size;
    jint    i;

    size = ENVPTR->GetArrayLength(ENVPAR (jarray) buf);

    wdata = (char**)HDcalloc((size_t)size + 1, sizeof(char*));
    if (!wdata) {
        h5JNIFatalError(env, "H5AwriteVL_str:  cannot allocate buffer");
    } /* end if */
    else {
        HDmemset(wdata, 0, (size_t)size * sizeof(char*));
        for (i = 0; i < size; ++i) {
            jstring obj = (jstring) ENVPTR->GetObjectArrayElement(ENVPAR (jobjectArray) buf, i);
            if (obj != 0) {
                jsize length = ENVPTR->GetStringUTFLength(ENVPAR obj);
                const char *utf8 = ENVPTR->GetStringUTFChars(ENVPAR obj, 0);

                if (utf8) {
                    wdata[i] = (char*)HDmalloc((size_t)length + 1);
                    if (wdata[i]) {
                        HDmemset(wdata[i], 0, ((size_t)length + 1));
                        HDstrncpy(wdata[i], utf8, (size_t)length);
                    } /* end if */
                } /* end if */

                ENVPTR->ReleaseStringUTFChars(ENVPAR obj, utf8);
                ENVPTR->DeleteLocalRef(ENVPAR obj);
            } /* end if */
        } /* end for (i = 0; i < size; ++i) */

        status = H5Awrite((hid_t)aid, (hid_t)tid, wdata);

        for (i = 0; i < size; i++) {
            if(wdata[i]) {
                HDfree(wdata[i]);
            } /* end if */
        } /* end for */
        HDfree(wdata);

        if (status < 0)
            h5libraryError(env);
    } /* end else */

    return (jint)status;
}
Exemplo n.º 6
0
herr_t
H5AreadVL_str
    (JNIEnv *env, hid_t aid, hid_t tid, jobjectArray buf)
{
    char  **strs;
    jstring jstr;
    jint    i;
    jint    n;
    hid_t   sid;
    hsize_t dims[H5S_MAX_RANK];
    herr_t  status = -1;

    n = ENVPTR->GetArrayLength(ENVPAR buf);
    strs =(char**)HDcalloc((size_t)n, sizeof(char*));

    if (strs == NULL) {
        h5JNIFatalError(env, "H5AreadVL_str:  failed to allocate buff for read variable length strings");
    } /* end if */
    else {
        status = H5Aread(aid, tid, strs);

        if (status < 0) {
            dims[0] = (hsize_t)n;
            sid = H5Screate_simple(1, dims, NULL);
            H5Dvlen_reclaim(tid, sid, H5P_DEFAULT, strs);
            H5Sclose(sid);
            HDfree(strs);
            h5JNIFatalError(env, "H5AreadVL_str: failed to read variable length strings");
        } /* end if */
        else {
            for (i=0; i < n; i++) {
                jstr = ENVPTR->NewStringUTF(ENVPAR strs[i]);
                ENVPTR->SetObjectArrayElement(ENVPAR buf, i, jstr);
                H5free_memory (strs[i]);
            } /* end for */

            /*
            for repeatedly reading a dataset with a large number of strs (e.g., 1,000,000 strings,
            H5Dvlen_reclaim() may crash on Windows because the Java GC will not be able to collect
            free space in time. Instead, use "H5free_memory(strs[i])" above to free individual strings
            after it is done.
            H5Dvlen_reclaim(tid, mem_sid, xfer_plist_id, strs);
            */

            HDfree(strs);
        } /* end else */
    } /* end else */

    return status;
} /* end H5AreadVL_str */
Exemplo n.º 7
0
/*
 *  h5libraryError()   determines the HDF-5 major error code
 *  and creates and throws the appropriate sub-class of
 *  HDF5LibraryException().  This routine should be called
 *  whenever a call to the HDF-5 library fails, i.e., when
 *  the return is -1.
 *
 *  Note:  This routine never returns from the 'throw',
 *  and the Java native method immediately raises the
 *  exception.
 */
jboolean
h5libraryError
    (JNIEnv *env)
{
    char       *args[2];
    const char *exception = NULL;
    char       *msg_str = NULL;
    int         num_errs = 0;
    hid_t       min_num;
    hid_t       maj_num;
    ssize_t     msg_size = 0;
    H5E_type_t  error_msg_type;
    jstring     str = NULL;
    hid_t       stk_id = -1;
    H5E_num_t   exceptionNumbers;

    exceptionNumbers.maj_num = 0;
    exceptionNumbers.min_num = 0;

    /* Save current stack contents for future use */
    stk_id = H5Eget_current_stack(); /* This will clear current stack  */
    if(stk_id >= 0)
        H5Ewalk2(stk_id, H5E_WALK_DOWNWARD, walk_error_callback, &exceptionNumbers);
    maj_num = exceptionNumbers.maj_num;
    min_num = exceptionNumbers.min_num;

    exception = defineHDF5LibraryException(maj_num);

    /* get the length of the name */
    msg_size = H5Eget_msg(min_num, NULL, NULL, 0);
    if(msg_size > 0) {
        msg_size++; /* add extra space for the null terminator */
        msg_str = (char*)HDcalloc((size_t)msg_size, sizeof(char));
        if(msg_str) {
            msg_size = H5Eget_msg(min_num, &error_msg_type, (char *)msg_str, (size_t)msg_size);
            str = ENVPTR->NewStringUTF(ENVPAR msg_str);
            HDfree(msg_str);
        } /* end if */
    } /* end if */
    else
        str = NULL;
    if(stk_id >= 0)
        H5Eset_current_stack(stk_id);

    args[0] = (char *)str;
    args[1] = 0;
    THROWEXCEPTION(exception, args);
} /* end h5libraryError() */
Exemplo n.º 8
0
/*--------------------------------------------------------------------------
 * NAME
 *    H5TS_cancel_count_inc
 *
 * USAGE
 *    H5TS_cancel_count_inc()
 *
 * RETURNS
 *    0 on success non-zero error code on error.
 *
 * DESCRIPTION
 *    Creates a cancelation counter for a thread if it is the first time
 *    the thread is entering the library.
 *
 *    if counter value is zero, then set cancelability type of the thread
 *    to PTHREAD_CANCEL_DISABLE as thread is entering the library and store
 *    the previous cancelability type into cancelation counter.
 *    Increase the counter value by 1.
 *
 * PROGRAMMER: Chee Wai LEE
 *            May 2, 2000
 *
 *--------------------------------------------------------------------------
 */
herr_t
H5TS_cancel_count_inc(void)
{
#ifdef  H5_HAVE_WIN_THREADS
    /* unsupported; just return 0 */
    return SUCCEED;
#else /* H5_HAVE_WIN_THREADS */
    H5TS_cancel_t *cancel_counter;
    herr_t ret_value = SUCCEED; 

    cancel_counter = (H5TS_cancel_t *)H5TS_get_thread_local_value(H5TS_cancel_key_g); 

    if (!cancel_counter) {
        /*
         * First time thread calls library - create new counter and associate
         * with key.
         * 
         * Don't use H5MM calls here since the destructor has to use HDfree in
         * order to avoid codestack calls.
         */
        cancel_counter = (H5TS_cancel_t *)HDcalloc(1, sizeof(H5TS_cancel_t));

        if (!cancel_counter) {
            H5E_push_stack(NULL, "H5TS_cancel_count_inc", __FILE__, __LINE__,
                H5E_ERR_CLS_g, H5E_RESOURCE, H5E_NOSPACE, "memory allocation failed");
            return FAIL;
        }

        ret_value = pthread_setspecific(H5TS_cancel_key_g, (void *)cancel_counter);
    }

    if (cancel_counter->cancel_count == 0)
        /* thread entering library */
        ret_value = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,
             &cancel_counter->previous_state);

    ++cancel_counter->cancel_count;

    return ret_value; 
#endif /* H5_HAVE_WIN_THREADS */
}
Exemplo n.º 9
0
/* not used yet */
void
create_binfile(char *name, off_t size)
{
    char *buf;
    int fd;
    int i;
    char *bp;

    fd = creat(name,0777);
    HDassert(fd >= 0);

    buf = HDcalloc(size,1);
    HDassert(buf);

    /* fill buf with pattern */
    bp = buf;
    for (i = 0; i < size; i++)
        *bp++ = (char) i & 0xff;

    HDwrite(fd,buf,size);

    HDclose(fd);
}
Exemplo n.º 10
0
/*
 * Function:    h5_set_info_object
 * Purpose:     Process environment variables setting to set up MPI Info
 *              object.
 * Return:      0 if all is fine; otherwise non-zero.
 * Programmer:  Albert Cheng, 2002/05/21.
 * Modifications:
 *          Bill Wendling, 2002/05/31
 *          Modified so that the HDF5_MPI_INFO environment variable can
 *          be a semicolon separated list of "key=value" pairings. Most
 *          of the code is to remove any whitespaces which might be
 *          surrounding the "key=value" pairs.
 */
int
h5_set_info_object(void)
{
    char  *envp;      /* environment pointer */
    int    ret_value=0;

    /* handle any MPI INFO hints via $HDF5_MPI_INFO */
    if ((envp = getenv("HDF5_MPI_INFO")) != NULL){
        char *next, *valp;

        valp = envp = next = HDstrdup(envp);

        if (!valp) return 0;

        /* create an INFO object if not created yet */
        if (h5_io_info_g == MPI_INFO_NULL)
            MPI_Info_create(&h5_io_info_g);

        do {
            size_t len;
            char *key_val, *endp, *namep;

            if (*valp == ';')
                valp++;

            /* copy key/value pair into temporary buffer */
            len = strcspn(valp, ";");
            next = &valp[len];
            key_val = (char *)HDcalloc(1, len + 1);

            /* increment the next pointer past the terminating semicolon */
            if (*next == ';')
                ++next;

            namep = HDstrncpy(key_val, valp, len);

            /* pass up any beginning whitespaces */
            while (*namep && (*namep == ' ' || *namep == '\t'))
                namep++;

            if (!*namep) continue; /* was all white space, so move to next k/v pair */

            /* eat up any ending white spaces */
            endp = &namep[HDstrlen(namep) - 1];

            while (endp && (*endp == ' ' || *endp == '\t'))
                *endp-- = '\0';

            /* find the '=' */
            valp = HDstrchr(namep, '=');

            if (valp != NULL) {     /* it's a valid key/value pairing */
                char *tmp_val = valp + 1;

                /* change '=' to \0, move valp down one */
                *valp-- = '\0';

                /* eat up ending whitespace on the "key" part */
                while (*valp == ' ' || *valp == '\t')
                    *valp-- = '\0';

                valp = tmp_val;

                /* eat up beginning whitespace on the "value" part */
                while (*valp == ' ' || *valp == '\t')
                    *valp++ = '\0';

                /* actually set the darned thing */
                if (MPI_SUCCESS != MPI_Info_set(h5_io_info_g, namep, valp)) {
                    printf("MPI_Info_set failed\n");
                    ret_value = -1;
                }
            }

            valp = next;
            HDfree(key_val);
        } while (next && *next);

        HDfree(envp);
    }

    return ret_value;
}
Exemplo n.º 11
0
static void
do_write_test(unsigned long file_size, unsigned long min_buf_size,
              unsigned long max_buf_size)
{
    uLongf src_len, total_len;
    struct timeval timer_start, timer_stop;
    double total_time;
    Bytef *src;

    for (src_len = min_buf_size; src_len <= max_buf_size; src_len <<= 1) {
        register unsigned long i, iters;

        iters = file_size / src_len;
        src = (Bytef *)HDcalloc(1, sizeof(Bytef) * src_len);

        if (!src) {
            cleanup();
            error("out of memory");
        }

        compression_time = 0.0;

        if (random_test)
            fill_with_random_data(src, src_len);

        HDfprintf(stdout, "Buffer size == ");

        if (src_len >= ONE_KB && (src_len % ONE_KB) == 0) {
            if (src_len >= ONE_MB && (src_len % ONE_MB) == 0) {
                HDfprintf(stdout, "%ldMB", src_len / ONE_MB);
            } else {
                HDfprintf(stdout, "%ldKB", src_len / ONE_KB);
            }
        } else {
            HDfprintf(stdout, "%ld", src_len);
        }

        HDfprintf(stdout, "\n");

        /* do uncompressed data write */
        HDgettimeofday(&timer_start, NULL);
        output = HDopen(filename, O_RDWR | O_CREAT, S_IRWXU);

        if (output == -1)
            error(HDstrerror(errno));

        for (i = 0; i <= iters; ++i) {
            Bytef *s_ptr = src;
            uLong s_len = src_len;

            /* loop to make sure we write everything out that we want to write */
            for (;;) {
                ssize_t rc = HDwrite(output, s_ptr, s_len);

                if (rc == -1)
                    error(HDstrerror(errno));

                if (rc == (ssize_t)s_len)
                    break;

                s_len -= rc;
                s_ptr += rc;
            }
        }

        HDclose(output);
        HDgettimeofday(&timer_stop, NULL);

        total_time = ((double)timer_stop.tv_sec +
                            ((double)timer_stop.tv_usec) / MICROSECOND) -
                     ((double)timer_start.tv_sec +
                            ((double)timer_start.tv_usec) / MICROSECOND);

        HDfprintf(stdout, "\tUncompressed Write Time: %.2fs\n", total_time);
        HDfprintf(stdout, "\tUncompressed Write Throughput: %.2fMB/s\n",
               MB_PER_SEC(file_size, total_time));

        HDunlink(filename);

        /* do compressed data write */
        output = HDopen(filename, O_RDWR | O_CREAT, S_IRWXU);

        if (output == -1)
            error(HDstrerror(errno));

        report_once_flag = 1;
        HDgettimeofday(&timer_start, NULL);

        for (total_len = 0; total_len < file_size; total_len += src_len)
            write_file(src, src_len);

        HDclose(output);
        HDgettimeofday(&timer_stop, NULL);

        total_time = ((double)timer_stop.tv_sec +
                            ((double)timer_stop.tv_usec) / MICROSECOND) -
                     ((double)timer_start.tv_sec +
                            ((double)timer_start.tv_usec) / MICROSECOND);

        HDfprintf(stdout, "\tCompressed Write Time: %.2fs\n", total_time);
        HDfprintf(stdout, "\tCompressed Write Throughput: %.2fMB/s\n",
               MB_PER_SEC(file_size, total_time));
        HDfprintf(stdout, "\tCompression Time: %gs\n", compression_time);

        HDunlink(filename);
        HDfree(src);
    }
}
Exemplo n.º 12
0
/*-------------------------------------------------------------------------
 * Function:	test_extend
 *
 * Purpose:	Creates an empty object and then writes to it in such a way
 *		as to always extend the object's domain without creating
 *		holes and without causing the object to become concave.
 *
 * Return:	Success:	SUCCEED
 *
 *		Failure:	FAIL
 *
 * Programmer:	Robb Matzke
 *		Wednesday, October 15, 1997
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static herr_t
test_extend(hid_t f, const char *prefix,
	    size_t nx, size_t ny, size_t nz)
{
    hid_t               dataset;        /* Dataset ID */
    hid_t               fspace;         /* Dataset's file dataspace */
    hid_t               mspace;         /* Dataset's memory dataspace */
    size_t		i, j, k, ctr;
    int			ndims;
    uint8_t		*buf = NULL, *check = NULL, *whole = NULL;
    char		dims[64], s[256], name[256];
    hsize_t		offset[3];
    hsize_t		max_corner[3];
    hsize_t		size[3];
    hsize_t		whole_size[3];
    hsize_t		nelmts;

    if (!nz) {
	if (!ny) {
	    ndims = 1;
	    ny = nz = 1;
	    sprintf(dims, "%lu", (unsigned long) nx);
	} else {
	    ndims = 2;
	    nz = 1;
	    sprintf(dims, "%lux%lu", (unsigned long) nx, (unsigned long) ny);
	}
    } else {
	ndims = 3;
	sprintf(dims, "%lux%lux%lu",
		(unsigned long) nx, (unsigned long) ny, (unsigned long) nz);
    }

    sprintf(s, "istore extend: %s", dims);
    TESTING(s);
    buf = (uint8_t *)HDmalloc(nx * ny * nz);
    check = (uint8_t *)HDmalloc(nx * ny * nz);
    whole = (uint8_t *)HDcalloc((size_t)1, nx * ny * nz);

    whole_size[0] = nx;
    whole_size[1] = ny;
    whole_size[2] = nz;
    max_corner[0] = 0;
    max_corner[1] = 0;
    max_corner[2] = 0;

    /* Build the new empty object */
    sprintf(name, "%s_%s", prefix, dims);
    if ((dataset=new_object(f, name, ndims, whole_size, whole_size)) < 0) {
	fprintf(stderr,"    Cannot create %u-d object `%s'\n", ndims, name);
	goto error;
    }

    /* Get dataset's dataspace */
    if((fspace=H5Dget_space(dataset)) < 0) TEST_ERROR;

    for (ctr = 0;
	 H5VM_vector_lt_u((unsigned)ndims, max_corner, whole_size);
	 ctr++) {

	/* Size and location */
	if (0 == ctr) {
	    offset[0] = offset[1] = offset[2] = 0;
	    size[0] = size[1] = size[2] = 1;
	    nelmts = 1;
	} else {
	    for (i=0, nelmts=1; i<(size_t)ndims; i++) {
		if (ctr % (size_t)ndims == i) {
		    offset[i] = max_corner[i];
		    size[i] = MIN(1, whole_size[i] - offset[i]);
		} else {
		    offset[i] = 0;
		    size[i] = max_corner[i];
		}
		nelmts *= size[i];
	    }
	}

#if 0
	if (0 == ctr)
	    fprintf(stderr,"\n");
	fprintf(stderr,"    Insert: ctr=%lu, corner=(%ld", (unsigned long)ctr, (long)offset[0]);
	if (ndims > 1)
	    fprintf(stderr,",%ld", (long)offset[1]);
	if (ndims > 2)
	    fprintf(stderr,",%ld", (long)offset[2]);
	fprintf(stderr,"), size=(%lu", (unsigned long)size[0]);
	if (ndims > 1)
	    fprintf(stderr,",%lu", (unsigned long)size[1]);
	if (ndims > 2)
	    fprintf(stderr,",%lu", (unsigned long)size[2]);
	fprintf(stderr,"), %lu element%s", (unsigned long)nelmts, 1 == nelmts ? "" : "s");
	if (0 == nelmts)
	    fprintf(stderr," *SKIPPED*");
	fprintf(stderr,"\n");
#endif

	/* Fill the source array */
	if (0 == nelmts) continue;
	HDmemset(buf, (signed)(128+ctr), (size_t)nelmts);

        /* Create dataspace for selection in memory */
        if((mspace=H5Screate_simple(1,&nelmts,NULL)) < 0) TEST_ERROR;

        /* Select region in file dataspace */
        if(H5Sselect_hyperslab(fspace,H5S_SELECT_SET,offset,NULL,size,NULL) < 0) TEST_ERROR;

	/* Write to disk */
	if (H5Dwrite(dataset, TEST_DATATYPE, mspace, fspace, H5P_DEFAULT, buf) < 0) {
	    H5_FAILED();
	    fprintf(stderr,"    Write failed: ctr=%lu\n", (unsigned long)ctr);
	    goto error;
	}

	/* Read from disk */
	HDmemset(check, 0xff, (size_t)nelmts);
	if (H5Dread(dataset, TEST_DATATYPE, mspace, fspace, H5P_DEFAULT, check) < 0) {
	    H5_FAILED();
	    fprintf(stderr,"    Read failed: ctr=%lu\n", (unsigned long)ctr);
	    goto error;
	}
	if (HDmemcmp(buf, check, (size_t)nelmts)) {
	    H5_FAILED();
	    fprintf(stderr,"    Read check failed: ctr=%lu\n", (unsigned long)ctr);
	    fprintf(stderr,"    Wrote:\n");
	    print_array(buf, (size_t)size[0], (size_t)size[1],
			(size_t)size[2]);
	    fprintf(stderr,"    Read:\n");
	    print_array(check, (size_t)size[0], (size_t)size[1],
			(size_t)size[2]);
	    goto error;
	}

        /* Close memory dataspace */
        if(H5Sclose(mspace) < 0) TEST_ERROR;

	/* Write to `whole' buffer for later checking */
	H5VM_hyper_copy((unsigned)ndims, size,
		       whole_size, offset, whole,	/*dst*/
		       size, H5VM_ZERO, buf);		/*src*/

	/* Update max corner */
	for (i=0; i<(size_t)ndims; i++)
	    max_corner[i] = MAX(max_corner[i], offset[i]+size[i]);
    }

    /* Now read the entire array back out and check it */
    HDmemset(buf, 0xff, nx * ny * nz);
    if (H5Dread(dataset, TEST_DATATYPE, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf) < 0) {
	H5_FAILED();
	fprintf(stderr,"    Read failed for whole array.\n");
	goto error;
    }
    for (i=0; i<nx; i++) {
	for (j=0; j<ny; j++) {
	    for (k=0; k<nz; k++) {
		if (whole[i*ny*nz + j*nz + k] != buf[i*ny*nz + j*nz + k]) {
		    H5_FAILED();
		    fprintf(stderr,"    Check failed at i=%lu", (unsigned long)i);
		    if (ndims > 1) {
			fprintf(stderr,", j=%lu", (unsigned long)j);
		    }
		    if (ndims > 2) {
			fprintf(stderr,", k=%lu", (unsigned long)k);
		    }
		    fprintf(stderr,"\n    Check array is:\n");
		    print_array(whole, nx, ny, nz);
		    fprintf(stderr,"    Value read is:\n");
		    print_array(buf, nx, ny, nz);
		    goto error;
		}
	    }
	}
    }

    /* Close dataset's dataspace */
    if(H5Sclose(fspace) < 0) TEST_ERROR;

    /* Close dataset */
    if(H5Dclose(dataset) < 0) TEST_ERROR;

    /* Free memory used */
    HDfree(buf);
    HDfree(check);
    HDfree(whole);

    PASSED();
    return SUCCEED;

error:
    HDfree(buf);
    HDfree(check);
    HDfree(whole);
    return FAIL;
}
Exemplo n.º 13
0
/****************************************************************
**
**  test_write_vl_string_attribute(): Test basic VL string code.
**      Tests writing VL strings as attributes
**
****************************************************************/
static void test_write_vl_string_attribute(void)
{
    hid_t file, root, dataspace, att;
    hid_t type;
    herr_t ret;
    char *string_att_check = NULL;

    /* Open the file */
    file = H5Fopen(DATAFILE, H5F_ACC_RDWR, H5P_DEFAULT);
    CHECK(file, FAIL, "H5Fopen");

    /* Create a datatype to refer to. */
    type = H5Tcopy (H5T_C_S1);
    CHECK(type, FAIL, "H5Tcopy");

    ret = H5Tset_size (type, H5T_VARIABLE);
    CHECK(ret, FAIL, "H5Tset_size");

    root = H5Gopen2(file, "/", H5P_DEFAULT);
    CHECK(root, FAIL, "H5Gopen2");

    dataspace = H5Screate(H5S_SCALAR);
    CHECK(dataspace, FAIL, "H5Screate");

    /* Test creating a "normal" sized string attribute */
    att = H5Acreate2(root, "test_scalar", type, dataspace, H5P_DEFAULT, H5P_DEFAULT);
    CHECK(att, FAIL, "H5Acreate2");

    ret = H5Awrite(att, type, &string_att);
    CHECK(ret, FAIL, "H5Awrite");

    ret = H5Aread(att, type, &string_att_check);
    CHECK(ret, FAIL, "H5Aread");

    if(HDstrcmp(string_att_check,string_att) != 0)
        TestErrPrintf("VL string attributes don't match!, string_att=%s, string_att_check=%s\n",string_att,string_att_check);

    H5free_memory(string_att_check);
    string_att_check = NULL;

    ret = H5Aclose(att);
    CHECK(ret, FAIL, "HAclose");

    /* Test creating a "large" sized string attribute */
    att = H5Acreate2(root, "test_scalar_large", type, dataspace, H5P_DEFAULT, H5P_DEFAULT);
    CHECK(att, FAIL, "H5Acreate2");

    string_att_write = (char*)HDcalloc((size_t)8192, sizeof(char));
    HDmemset(string_att_write, 'A', (size_t)8191);

    ret = H5Awrite(att, type, &string_att_write);
    CHECK(ret, FAIL, "H5Awrite");

    ret = H5Aread(att, type, &string_att_check);
    CHECK(ret, FAIL, "H5Aread");

    if(HDstrcmp(string_att_check,string_att_write) != 0)
        TestErrPrintf("VL string attributes don't match!, string_att_write=%s, string_att_check=%s\n",string_att_write,string_att_check);

    H5free_memory(string_att_check);
    string_att_check = NULL;

    /* The attribute string written is freed below, in the test_read_vl_string_attribute() test */
    /* HDfree(string_att_write); */

    ret = H5Aclose(att);
    CHECK(ret, FAIL, "HAclose");

    ret = H5Gclose(root);
    CHECK(ret, FAIL, "H5Gclose");

    ret = H5Tclose(type);
    CHECK(ret, FAIL, "H5Tclose");

    ret = H5Sclose(dataspace);
    CHECK(ret, FAIL, "H5Sclose");

    ret = H5Fclose(file);
    CHECK(ret, FAIL, "H5Fclose");

    return;
}
Exemplo n.º 14
0
/*-------------------------------------------------------------------------
 * Function:  ensure_filter_works
 *
 * Purpose:   Tests writing entire data and partial data with filters
 *
 * Return:    SUCCEED/FAIL
 *
 *-------------------------------------------------------------------------
 */
static herr_t
ensure_filter_works(hid_t fid, const char *name, hid_t dcpl_id)
{
    hid_t           did = -1;                       /* Dataset ID                                   */
    hid_t           dxpl_id = -1;                   /* Dataset xfer property list ID                */
    hid_t           write_dxpl_id = -1;             /* Dataset xfer property list ID for writing    */
    hid_t           sid = -1;                       /* Dataspace ID                                 */
    void           *tconv_buf = NULL;               /* Temporary conversion buffer                  */
    int           **orig = NULL;                    /* Data written to the dataset                  */
    int           **read = NULL;                    /* Data read from the dataset                   */
    size_t          r, c;                           /* Data rows and columns                        */
    size_t          hs_r, hs_c, hs_offr, hs_offc;   /* Hypserslab sizes and offsets                 */
    size_t          i, j;                           /* Local index variables                        */
    int             n = 0;                          /* Value written to point array                 */
    hbool_t         are_same;                       /* Output from dataset compare function         */
    int          ***save_array = NULL;              /* (Global) array where the final data go       */

    /* initialize */
    r = (size_t)sizes_g[0];
    c = (size_t)sizes_g[1];

    /* Create the data space */
    if ((sid = H5Screate_simple(2, sizes_g, NULL)) < 0)
        TEST_ERROR;

    /* Allocate memory for the data buffers
     * We're using the hacky way of doing 2D arrays that uses a
     * single data buffer but which allows normal 2D access.
     */
    if (allocate_and_init_2D_array(&orig, sizes_g, NULL) < 0)
        TEST_ERROR;
    if (allocate_and_init_2D_array(&read, sizes_g, NULL) < 0)
        TEST_ERROR;

    /* Create a small conversion buffer to test strip mining. We
     * might as well test all we can!
     */
    if ((dxpl_id = H5Pcreate(H5P_DATASET_XFER)) < 0)
        TEST_ERROR;
    if (NULL == (tconv_buf = HDcalloc((size_t)1000, sizeof(char))))
        TEST_ERROR;
    if (H5Pset_buffer(dxpl_id, (size_t)1000, tconv_buf, NULL) < 0)
        TEST_ERROR;
    if ((write_dxpl_id = H5Pcopy(dxpl_id)) < 0)
        TEST_ERROR;

    TESTING("    filters (setup)");

    /* Check if all the filters are available */
    if (H5Pall_filters_avail(dcpl_id) != TRUE)
        TEST_ERROR;

    /* Create the dataset */
    if ((did = H5Dcreate2(fid, name, H5T_NATIVE_INT, sid, H5P_DEFAULT, dcpl_id, H5P_DEFAULT)) < 0)
        TEST_ERROR;

    PASSED();

    /*----------------------------------------------------------------------
     * STEP 1: Read uninitialized data.  It should be zero.
     *----------------------------------------------------------------------
     */
    TESTING("    filters (uninitialized read)");

    if (H5Dread(did, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, dxpl_id, *read) < 0)
        TEST_ERROR;

    /* The input buffer was calloc'd and has not been initialized yet */
    if (compare_2D_arrays(orig, read, sizes_g, &are_same) < 0)
        TEST_ERROR;
    if (FALSE == are_same)
        TEST_ERROR;

    PASSED();

    /*----------------------------------------------------------------------
     * STEP 2: Test filters by setting up a chunked dataset and writing
     * to it.
     *----------------------------------------------------------------------
     */
    TESTING("    filters (write)");

    n = 0;
    for (i = 0; i < r; i++)
        for (j = 0; j < c; j++)
            orig[i][j] = n++;

    if (H5Dwrite(did, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, write_dxpl_id, *orig) < 0)
        TEST_ERROR;

    PASSED();

    /*----------------------------------------------------------------------
     * STEP 3: Try to read the data we just wrote.
     *----------------------------------------------------------------------
     */
    TESTING("    filters (read)");

    /* Read the dataset back */
    if (H5Dread(did, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, dxpl_id, *read) < 0)
        TEST_ERROR;

    /* Check that the values read are the same as the values written */
    if (compare_2D_arrays(orig, read, sizes_g, &are_same) < 0)
        TEST_ERROR;
    if (FALSE == are_same)
        TEST_ERROR;

    PASSED();

    /*----------------------------------------------------------------------
     * STEP 4: Write new data over the top of the old data.  The new data is
     * random thus not very compressible, and will cause the chunks to move
     * around as they grow.  We only change values for the left half of the
     * dataset although we rewrite the whole thing.
     *----------------------------------------------------------------------
     */
    TESTING("    filters (modify)");

    for (i = 0; i < r; i++)
        for (j = 0; j < c / 2; j++)
            orig[i][j] = (int)HDrandom() % RANDOM_LIMIT;

    if (H5Dwrite(did, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, write_dxpl_id, *orig) < 0)
        TEST_ERROR;

    /* Read the dataset back and check it */
    if (H5Dread(did, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, dxpl_id, *read) < 0)
        TEST_ERROR;

    /* Check that the values read are the same as the values written */
    if (compare_2D_arrays(orig, read, sizes_g, &are_same) < 0)
        TEST_ERROR;
    if (FALSE == are_same)
        TEST_ERROR;

    PASSED();

    /*----------------------------------------------------------------------
     * STEP 5: Close the dataset and then open it and read it again.  This
     * insures that the filters message is picked up properly from the
     * object header.
     *----------------------------------------------------------------------
     */
    TESTING("    filters (re-open)");

    if (H5Dclose(did) < 0)
        TEST_ERROR;
    if ((did = H5Dopen2(fid, name, H5P_DEFAULT)) < 0)
        TEST_ERROR;

    if (H5Dread(did, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, dxpl_id, *read) < 0)
        TEST_ERROR;

    /* Check that the values read are the same as the values written */
    if (compare_2D_arrays(orig, read, sizes_g, &are_same) < 0)
        TEST_ERROR;
    if (FALSE == are_same)
        TEST_ERROR;

    PASSED();

    /*----------------------------------------------------------------------
     * STEP 6: Test partial I/O by writing to and then reading from a
     * hyperslab of the dataset.  The hyperslab does not line up on chunk
     * boundaries (we know that case already works from above tests).
     *----------------------------------------------------------------------
     */
    TESTING("    filters (partial I/O)");

    hs_r = (size_t)hs_sizes_g[0];
    hs_c = (size_t)hs_sizes_g[1];
    hs_offr = (size_t)hs_offsets_g[0];
    hs_offc = (size_t)hs_offsets_g[1];
    for (i = 0; i < hs_r; i++)
        for (j = 0; j < hs_c; j++)
            orig[hs_offr + i][hs_offc + j] = (int)HDrandom() % RANDOM_LIMIT;

    if (H5Sselect_hyperslab(sid, H5S_SELECT_SET, hs_offsets_g, NULL, hs_sizes_g, NULL) < 0)
        TEST_ERROR;

    /* Use the "read" DXPL because partial I/O on corrupted data test
     * needs to ignore errors during writing
     */
    if (H5Dwrite(did, H5T_NATIVE_INT, sid, sid, dxpl_id, *orig) < 0)
        TEST_ERROR;

    if (H5Dread(did, H5T_NATIVE_INT, sid, sid, dxpl_id, *read) < 0)
        TEST_ERROR;

    /* Check that the values read are the same as the values written */
    if (compare_2D_arrays(orig, read, sizes_g, &are_same) < 0)
        TEST_ERROR;
    if (FALSE == are_same)
        TEST_ERROR;

    PASSED();

    /* Save the data written to the file for later comparison when the file
     * is reopened for read test.
     */
    if (!HDstrcmp(name, DSET_DEFLATE_NAME))
        save_array = &orig_deflate_g;
    else if (!HDstrcmp(name, DSET_FILTER1_NAME))
        save_array = &orig_dynlib1_g;
    else if (!HDstrcmp(name, DSET_FILTER2_NAME))
        save_array = &orig_dynlib2_g;
    else if (!HDstrcmp(name, DSET_FILTER3_NAME))
        save_array = &orig_dynlib4_g;
    else
        TEST_ERROR;
    if (allocate_and_init_2D_array(save_array, sizes_g, orig) < 0)
        TEST_ERROR;

    /* Clean up and exit */
    if (H5Dclose(did) < 0)
        TEST_ERROR;
    if (H5Sclose(sid) < 0)
        TEST_ERROR;
    if (H5Pclose(dxpl_id) < 0)
        TEST_ERROR;
    if (H5Pclose(write_dxpl_id) < 0)
        TEST_ERROR;

    free_2D_array(&orig);
    free_2D_array(&read);

    HDfree(tconv_buf);

    return SUCCEED;

error:
    /* Clean up objects used for this test */
    H5E_BEGIN_TRY {
        H5Dclose(did);
        H5Sclose(sid);
        H5Pclose(dxpl_id);
        H5Pclose(write_dxpl_id);
    } H5E_END_TRY

    /* NULLs are okay here */
    free_2D_array(&orig);
    free_2D_array(&read);

    if (tconv_buf)
        HDfree(tconv_buf);

    return FAIL;
} /* end ensure_filter_works() */
Exemplo n.º 15
0
herr_t
H5AreadVL_asstr
    (JNIEnv *env, hid_t aid, hid_t tid, jobjectArray buf)
{
    jint    i;
    jint    n;
    hid_t   sid;
    jstring jstr;
    h5str_t h5str;
    hvl_t  *rdata;
    hsize_t dims[H5S_MAX_RANK];
    size_t  size;
    size_t  max_len = 0;
    herr_t  status = -1;

    /* Get size of string array */
    n = ENVPTR->GetArrayLength(ENVPAR buf);
    /* we will need to read n number of hvl_t structures */
    rdata = (hvl_t*)HDcalloc((size_t)n, sizeof(hvl_t));
    if (rdata == NULL) {
        h5JNIFatalError(env, "H5AreadVL_asstr:  failed to allocate buff for read");
    } /* end if */
    else {
        status = H5Aread(aid, tid, rdata);

        if (status < 0) {
            dims[0] = (hsize_t)n;
            sid = H5Screate_simple(1, dims, NULL);
            H5Dvlen_reclaim(tid, sid, H5P_DEFAULT, rdata);
            H5Sclose(sid);
            HDfree(rdata);
            h5JNIFatalError(env, "H5AreadVL_asstr: failed to read data");
        } /* end if */
        else {
            /* calculate the largest size of all the hvl_t structures read */
            max_len = 1;
            for (i=0; i < n; i++) {
                if ((rdata + i)->len > max_len)
                    max_len = (rdata + i)->len;
            }

            /* create one malloc to hold largest element */
            size = H5Tget_size(tid) * max_len;
            HDmemset(&h5str, 0, sizeof(h5str_t));
            h5str_new(&h5str, 4 * size);

            if (h5str.s == NULL) {
                dims[0] = (hsize_t)n;
                sid = H5Screate_simple(1, dims, NULL);
                H5Dvlen_reclaim(tid, sid, H5P_DEFAULT, rdata);
                H5Sclose(sid);
                HDfree(rdata);
                h5JNIFatalError(env, "H5AreadVL_asstr:  failed to allocate buf");
            } /* end if */
            else {
                H5T_class_t tclass = H5Tget_class(tid);
                /* convert each element to char string */
                for (i=0; i < n; i++) {
                    h5str.s[0] = '\0';
                    h5str_vlsprintf(&h5str, aid, tid, rdata+i, 0);
                    jstr = ENVPTR->NewStringUTF(ENVPAR h5str.s);
                    ENVPTR->SetObjectArrayElement(ENVPAR buf, i, jstr);
                } /* end for */
                h5str_free(&h5str);

                dims[0] = (hsize_t)n;
                sid = H5Screate_simple(1, dims, NULL);
                H5Dvlen_reclaim(tid, sid, H5P_DEFAULT, rdata);
                H5Sclose(sid);
                HDfree(rdata);
            } /* end else */
        } /* end else */
    } /* end else */

    return status;
}
Exemplo n.º 16
0
/*-------------------------------------------------------------------------
 * Function:	test_fill
 *
 * Purpose:	Tests the H5VM_hyper_fill() function.
 *
 * Return:	Success:	SUCCEED
 *
 *		Failure:	FAIL
 *
 * Programmer:	Robb Matzke
 *		Saturday, October 11, 1997
 *
 *-------------------------------------------------------------------------
 */
static herr_t
test_fill(size_t nx, size_t ny, size_t nz,
	  size_t di, size_t dj, size_t dk,
	  size_t ddx, size_t ddy, size_t ddz)
{
    uint8_t *dst = NULL;        /*destination array		*/
    hsize_t hs_size[3];         /*hyperslab size		*/
    hsize_t dst_size[3];        /*destination total size	*/
    hsize_t dst_offset[3];      /*offset of hyperslab in dest   */
    unsigned ref_value;         /*reference value		*/
    unsigned acc;               /*accumulator		    	*/
    size_t i, j, k, dx, dy, dz; /*counters		   	*/
    size_t u, v, w;
    unsigned ndims;             /*hyperslab dimensionality	*/
    char dim[64], s[256];       /*temp string		    	*/
    unsigned fill_value;        /*fill value		    	*/

    /*
     * Dimensionality.
     */
    if(0 == nz) {
        if(0 == ny) {
            ndims = 1;
            ny = nz = 1;
            sprintf(dim, "%lu", (unsigned long) nx);
        } /* end if */
        else {
            ndims = 2;
            nz = 1;
            sprintf(dim, "%lux%lu", (unsigned long) nx, (unsigned long) ny);
        } /* end else */
    } /* end if */
    else {
        ndims = 3;
        sprintf(dim, "%lux%lux%lu", (unsigned long) nx, (unsigned long) ny,
                (unsigned long) nz);
    } /* end else */
    sprintf(s, "Testing hyperslab fill %-11s variable hyperslab", dim);
    printf("%-70s", s);
    fflush(stdout);

    /* Allocate array */
    if(NULL == (dst = (uint8_t *)HDcalloc((size_t)1, nx * ny * nz)))
        TEST_ERROR
    
    init_full(dst, nx, ny, nz);

    for(i = 0; i < nx; i += di) {
        for(j = 0; j < ny; j += dj) {
            for(k = 0; k < nz; k += dk) {
                for(dx = 1; dx <= nx - i; dx += ddx) {
                    for(dy = 1; dy <= ny - j; dy += ddy) {
                        for(dz = 1; dz <= nz - k; dz += ddz) {

                            /* Describe the hyperslab */
                            dst_size[0] = nx;
                            dst_size[1] = ny;
                            dst_size[2] = nz;
                            dst_offset[0] = i;
                            dst_offset[1] = j;
                            dst_offset[2] = k;
                            hs_size[0] = dx;
                            hs_size[1] = dy;
                            hs_size[2] = dz;

                            for(fill_value = 0; fill_value < 256; fill_value += 64) {
                                /*
                                 * Initialize the full array, then subtract the
                                 * original * fill values and add the new ones.
                                 */
                                ref_value = init_full(dst, nx, ny, nz);
                                for(u = (size_t)dst_offset[0]; u < dst_offset[0] + dx; u++)
                                    for(v = (size_t)dst_offset[1]; v < dst_offset[1] + dy; v++)
                                        for(w = (size_t)dst_offset[2]; w < dst_offset[2] + dz; w++)
                                            ref_value -= dst[u * ny * nz + v * nz + w];
                                ref_value += fill_value * (unsigned)dx * (unsigned)dy * (unsigned)dz;

                                /* Fill the hyperslab with some value */
                                H5VM_hyper_fill(ndims, hs_size, dst_size, dst_offset, dst, fill_value);

                                /*
                                 * Sum the array and compare it to the
                                 * reference value.
                                 */
                                acc = 0;
                                for(u = 0; u < nx; u++)
                                    for(v = 0; v < ny; v++)
                                        for(w = 0; w < nz; w++)
                                            acc += dst[u * ny * nz + v * nz + w];

                                if(acc != ref_value) {
                                    H5_FAILED()
                                    if(!HDisatty(1)) {
                                        /*
                                         * Print debugging info unless output
                                         * is going directly to a terminal.
                                         */
                                        AT();
                                        printf("   acc != ref_value\n");
                                        printf("   i=%lu, j=%lu, k=%lu, "
                                            "dx=%lu, dy=%lu, dz=%lu, "
                                            "fill=%d\n", (unsigned long)i,
                                                (unsigned long)j,
                                                (unsigned long)k,
                                                (unsigned long)dx,
                                                (unsigned long)dy,
                                                (unsigned long)dz, fill_value);
                                        print_ref(nx, ny, nz);
                                        printf("\n   Result is:\n");
                                        print_array(dst, nx, ny, nz);
                                    } /* end if */
                                    goto error;
                                } /* end if */
                            } /* end for */
                        } /* end for */
                    } /* end for */
                } /* end for */
            } /* end for */
        } /* end for */
    } /* end for */
Exemplo n.º 17
0
/*-------------------------------------------------------------------------
 * Function: parse_command_line
 *
 * Purpose: Parses command line and sets up global variable to control output
 *
 * Return: Success: 0
 *
 * Failure: -1
 *
 * Programmer: Elena Pourmal
 *             Saturday, August 12, 2006
 *
 *-------------------------------------------------------------------------
 */
static struct handler_t *
parse_command_line(int argc, const char *argv[])
{
    int                opt, i;
    struct handler_t   *hand = NULL;

    /* Allocate space to hold the command line info */
    if((hand = (struct handler_t *)HDcalloc((size_t)argc, sizeof(struct handler_t)))==NULL) {
        error_msg("unable to parse command line arguments \n");
        goto error;
    }

    /* parse command line options */
    while ((opt = get_option(argc, argv, s_opts, l_opts)) != EOF) {
        switch ((char)opt) {
            case 'h':
                usage(h5tools_getprogname());
                h5tools_setstatus(EXIT_SUCCESS);
                if (hand) {
                    for (i = 0; i < argc; i++)
                        if(hand[i].obj) {
                            free(hand[i].obj);
                            hand[i].obj=NULL;
                        }

                    free(hand);
                    hand = NULL;
                }
                goto done;
                break;

            case 'V':
                print_version(h5tools_getprogname());
                h5tools_setstatus(EXIT_SUCCESS);
                if (hand) {
                    for (i = 0; i < argc; i++)
                        if(hand[i].obj) {
                            free(hand[i].obj);
                            hand[i].obj=NULL;
                        }

                    free(hand);
                    hand = NULL;
                }
                goto done;
                break;

            case 'F':
                display_all = FALSE;
                display_file_metadata = TRUE;
                break;

            case 'f':
                display_all = FALSE;
                display_file = TRUE;
                break;

            case 'G':
                display_all = FALSE;
                display_group_metadata = TRUE;
                break;

            case 'g':
                display_all = FALSE;
                display_group = TRUE;
                break;

            case 'D':
                display_all = FALSE;
                display_dset_metadata = TRUE;
                break;

            case 'd':
                display_all = FALSE;
                display_dset = TRUE;
                break;

            case 'T':
                display_all = FALSE;
                display_dset_dtype_meta = TRUE;
                break;

            case 'A':
                display_all = FALSE;
                display_attr = TRUE;
                break;

            case 'S':
                display_all = FALSE;
                display_summary = TRUE;
                break;

            case 'O':
                display_all = FALSE;
                display_object = TRUE;
                for(i = 0; i < argc; i++)
                    if(!hand[i].obj) {
                        hand[i].obj = HDstrdup(opt_arg);
                        break;
                    } /* end if */
                break;

            default:
                usage(h5tools_getprogname());
                h5tools_setstatus(EXIT_FAILURE);
                goto error;
        } /* end switch */
    } /* end while */

    /* check for file name to be processed */
    if (argc <= opt_ind) {
        error_msg("missing file name\n");
        usage(h5tools_getprogname());
        h5tools_setstatus(EXIT_FAILURE);
        goto error;
    } /* end if */

done:
    return hand;

error:
    if (hand) {
        for (i = 0; i < argc; i++)
            if(hand[i].obj) {
                free(hand[i].obj);
                hand[i].obj=NULL;
            }

        free(hand);
        hand = NULL;
    }
    h5tools_setstatus(EXIT_FAILURE);

    return hand;
}
Exemplo n.º 18
0
int
main (int argc, const char *argv[])
{
    hid_t        fid_src = -1;
    hid_t        fid_dst = -1;
    unsigned     flag = 0;
    unsigned     verbose = 0;
    unsigned     parents = 0;
    hid_t        ocpl_id = (-1);          /* Object copy property list */
    hid_t        lcpl_id = (-1);          /* Link creation property list */
    int          opt;
    int          li_ret;
    h5tool_link_info_t linkinfo;

    h5tools_setprogname(PROGRAMNAME);
    h5tools_setstatus(EXIT_SUCCESS);

    /* initialize h5tools lib */
    h5tools_init();

    /* init linkinfo struct */
    HDmemset(&linkinfo, 0, sizeof(h5tool_link_info_t));

    /* Check for no command line parameters */
    if(argc == 1) 
    {
        usage();
        leave(EXIT_FAILURE);
    } /* end if */

    /* parse command line options */
    while ((opt = get_option(argc, argv, s_opts, l_opts)) != EOF)
    {
        switch ((char)opt)
        {
        case 'd':
            oname_dst = HDstrdup(opt_arg);
            break;

        case 'f':
            /* validate flag */
            if (parse_flag(opt_arg,&flag)<0)
            {
                usage();
                leave(EXIT_FAILURE);
            }
            str_flag = HDstrdup(opt_arg);
            break;

        case 'h':
            usage();
            leave(EXIT_SUCCESS);
            break;

        case 'i':
            fname_src = HDstrdup(opt_arg);
            break;

        case 'o':
            fname_dst = HDstrdup(opt_arg);
            break;

        case 'p':
            parents = 1;
            break;

        case 's':
            oname_src = HDstrdup(opt_arg);
            break;

        case 'V':
            print_version(h5tools_getprogname());
            leave(EXIT_SUCCESS);
            break;

        case 'v':
            verbose = 1;
            break;

        default:
            usage();
            leave(EXIT_FAILURE);
        }
    } /* end of while */

/*-------------------------------------------------------------------------
 * check for missing file/object names
 *-------------------------------------------------------------------------*/

    if (fname_src==NULL)
    {
        error_msg("Input file name missing\n");
        usage();
        leave(EXIT_FAILURE);
    }

    if (fname_dst==NULL)
    {
        error_msg("Output file name missing\n");
        usage();
        leave(EXIT_FAILURE);
    }

    if (oname_src==NULL)
    {
        error_msg("Source object name missing\n");
        usage();
        leave(EXIT_FAILURE);
    }

    if (oname_dst==NULL)
    {
        error_msg("Destination object name missing\n");
        usage();
        leave(EXIT_FAILURE);
    }

   /*-------------------------------------------------------------------------
    * open output file
    *-------------------------------------------------------------------------*/

    /* Attempt to open an existing HDF5 file first. Need to open the dst file
       before the src file just in case that the dst and src are the same file
     */
    fid_dst = h5tools_fopen(fname_dst, H5F_ACC_RDWR, H5P_DEFAULT, NULL, NULL, 0);

   /*-------------------------------------------------------------------------
    * open input file
    *-------------------------------------------------------------------------*/

    fid_src = h5tools_fopen(fname_src, H5F_ACC_RDONLY, H5P_DEFAULT, NULL, NULL, 0);

   /*-------------------------------------------------------------------------
    * test for error in opening input file
    *-------------------------------------------------------------------------*/
    if (fid_src==-1)
    {
        error_msg("Could not open input file <%s>...Exiting\n", fname_src);
        leave(EXIT_FAILURE);
    }


   /*-------------------------------------------------------------------------
    * create an output file when failed to open it
    *-------------------------------------------------------------------------*/

    /* If we couldn't open an existing file, try creating file */
    /* (use "EXCL" instead of "TRUNC", so we don't blow away existing non-HDF5 file) */
    if(fid_dst < 0)
        fid_dst = H5Fcreate(fname_dst, H5F_ACC_EXCL, H5P_DEFAULT, H5P_DEFAULT);

   /*-------------------------------------------------------------------------
    * test for error in opening output file
    *-------------------------------------------------------------------------*/
    if (fid_dst==-1)
    {
        error_msg("Could not open output file <%s>...Exiting\n", fname_dst);
        leave(EXIT_FAILURE);
    }

   /*-------------------------------------------------------------------------
    * print some info
    *-------------------------------------------------------------------------*/

    if (verbose)
    {
        printf("Copying file <%s> and object <%s> to file <%s> and object <%s>\n",
        fname_src, oname_src, fname_dst, oname_dst);
        if (flag) {
            HDassert(str_flag);
            printf("Using %s flag\n", str_flag);
        }
    }


   /*-------------------------------------------------------------------------
    * create property lists for copy
    *-------------------------------------------------------------------------*/

    /* create property to pass copy options */
    if ( (ocpl_id = H5Pcreate(H5P_OBJECT_COPY)) < 0)
        goto error;

    /* set options for object copy */
    if (flag)
    {
        if ( H5Pset_copy_object(ocpl_id, flag) < 0)
            goto error;
    }

    /* Create link creation property list */
    if((lcpl_id = H5Pcreate(H5P_LINK_CREATE)) < 0) {
        error_msg("Could not create link creation property list\n");
        goto error;
    } /* end if */

    /* Check for creating intermediate groups */
    if(parents) {
        /* Set the intermediate group creation property */
        if(H5Pset_create_intermediate_group(lcpl_id, 1) < 0) {
            error_msg("Could not set property for creating parent groups\n");
            goto error;
        } /* end if */

        /* Display some output if requested */
        if(verbose)
            printf("%s: Creating parent groups\n", h5tools_getprogname());
    } /* end if */
    else /* error, if parent groups doesn't already exist in destination file */
    {
        size_t        i, len;

        len = HDstrlen(oname_dst);        

        /* check if all the parents groups exist. skip root group */
        for (i = 1; i < len; i++)
        {
            if ('/'==oname_dst[i])
            {
                char         *str_ptr;

                str_ptr = (char *)HDcalloc(i + 1, sizeof(char));
                HDstrncpy(str_ptr, oname_dst, i);
                str_ptr[i]='\0';
                if (H5Lexists(fid_dst, str_ptr, H5P_DEFAULT) <= 0)
                {
                    error_msg("group <%s> doesn't exist. Use -p to create parent groups.\n", str_ptr);
                    HDfree(str_ptr);
                    goto error;
                }
                HDfree(str_ptr);
            }
        }
    }

   /*-------------------------------------------------------------------------
    * do the copy
    *-------------------------------------------------------------------------*/
 
    if(verbose)
        linkinfo.opt.msg_mode = 1;
 
    li_ret = H5tools_get_symlink_info(fid_src, oname_src, &linkinfo, 1);
    if (li_ret == 0) /* dangling link */
    {
        if(H5Lcopy(fid_src, oname_src, 
                   fid_dst, oname_dst,
                   H5P_DEFAULT, H5P_DEFAULT) < 0)
            goto error;
    }
    else /* valid link */
    {
        if (H5Ocopy(fid_src,          /* Source file or group identifier */
                  oname_src,        /* Name of the source object to be copied */
                  fid_dst,          /* Destination file or group identifier  */
                  oname_dst,        /* Name of the destination object  */
                  ocpl_id,          /* Object copy property list */
                  lcpl_id)<0)       /* Link creation property list */
            goto error;
    }

    /* free link info path */
    if (linkinfo.trg_path)
        HDfree(linkinfo.trg_path);

    /* close propertis */
    if(H5Pclose(ocpl_id)<0)
        goto error;
    if(H5Pclose(lcpl_id)<0)
        goto error;

    /* close files */
    if (H5Fclose(fid_src)<0)
        goto error;
    if (H5Fclose(fid_dst)<0)
        goto error;

    leave(EXIT_SUCCESS);

error:
    printf("Error in copy...Exiting\n");

    /* free link info path */
    if (linkinfo.trg_path)
        HDfree(linkinfo.trg_path);

 H5E_BEGIN_TRY {
    H5Pclose(ocpl_id);
    H5Pclose(lcpl_id);
    H5Fclose(fid_src);
    H5Fclose(fid_dst);
 } H5E_END_TRY;

 leave(EXIT_FAILURE);
}
Exemplo n.º 19
0
/*-------------------------------------------------------------------------
 * Function: H5tools_get_symlink_info
 *
 * Purpose: Get symbolic link (soft, external) info and its target object type 
            (dataset, group, named datatype) and path, if exist
 *
 * Patameters:
 *  - [IN]  fileid : link file id
 *  - [IN]  linkpath : link path
 *  - [OUT] link_info: returning target object info (h5tool_link_info_t)
 *
 * Return: 
 *   2 : given pathname is object 
 *   1 : Succed to get link info.  
 *   0 : Detected as a dangling link
 *  -1 : H5 API failed.
 *
 * NOTE:
 *  link_info->trg_path must be freed out of this function
 *
 * Programmer: Jonathan Kim
 *
 * Date: Feb 8, 2010
 *-------------------------------------------------------------------------*/
int
H5tools_get_symlink_info(hid_t file_id, const char * linkpath, h5tool_link_info_t *link_info,
    hbool_t get_obj_type)
{
    htri_t l_ret;
    H5O_info_t trg_oinfo;
    hid_t fapl = H5P_DEFAULT;
    hid_t lapl = H5P_DEFAULT;
    int ret = -1; /* init to fail */

    /* init */
    link_info->trg_type = H5O_TYPE_UNKNOWN;

    /* if path is root, return group type */
    if(!HDstrcmp(linkpath,"/"))
    {
        link_info->trg_type = H5O_TYPE_GROUP;
        ret = 2;
        goto out;
    }

    /* check if link itself exist */
    if(H5Lexists(file_id, linkpath, H5P_DEFAULT) <= 0) {
        if(link_info->opt.msg_mode == 1)
            parallel_print("Warning: link <%s> doesn't exist \n",linkpath);
        goto out;
    } /* end if */

    /* get info from link */
    if(H5Lget_info(file_id, linkpath, &(link_info->linfo), H5P_DEFAULT) < 0) {
        if(link_info->opt.msg_mode == 1)
            parallel_print("Warning: unable to get link info from <%s>\n",linkpath);
        goto out;
    } /* end if */

    /* given path is hard link (object) */
    if(link_info->linfo.type == H5L_TYPE_HARD) {
        ret = 2;
        goto out;
    } /* end if */

    /* trg_path must be freed out of this function when finished using */
    link_info->trg_path = (char*)HDcalloc(link_info->linfo.u.val_size, sizeof(char));
    HDassert(link_info->trg_path);

    /* get link value */
    if(H5Lget_val(file_id, linkpath, (void *)link_info->trg_path, link_info->linfo.u.val_size, H5P_DEFAULT) < 0) {
        if(link_info->opt.msg_mode == 1)
            parallel_print("Warning: unable to get link value from <%s>\n",linkpath);
        goto out;
    } /* end if */

    /*-----------------------------------------------------
     * if link type is external link use different lapl to 
     * follow object in other file
     */
    if(link_info->linfo.type == H5L_TYPE_EXTERNAL) {
        if((fapl = H5Pcreate(H5P_FILE_ACCESS)) < 0)
            goto out;
        if(H5Pset_fapl_sec2(fapl) < 0)
            goto out;
        if((lapl = H5Pcreate(H5P_LINK_ACCESS)) < 0)
            goto out;
        if(H5Pset_elink_fapl(lapl, fapl) < 0)
            goto out;
    } /* end if */

    /* Check for retrieving object info */
    if(get_obj_type) {
        /*--------------------------------------------------------------
         * if link's target object exist, get type
         */
         /* check if target object exist */
        l_ret = H5Oexists_by_name(file_id, linkpath, lapl);
        
        /* detect dangling link */
        if(l_ret == FALSE) {
            ret = 0;
            goto out;
        } /* end if */
        /* function failed */
        else if(l_ret < 0)
            goto out;    

        /* get target object info */
        if(H5Oget_info_by_name(file_id, linkpath, &trg_oinfo, lapl) < 0) {
            if(link_info->opt.msg_mode == 1)
                parallel_print("Warning: unable to get object information for <%s>\n", linkpath);
            goto out;
        } /* end if */

        /* check unknown type */
        if(trg_oinfo.type < H5O_TYPE_GROUP || trg_oinfo.type >=H5O_TYPE_NTYPES) {
            if(link_info->opt.msg_mode == 1)
                parallel_print("Warning: target object of <%s> is unknown type\n", linkpath);
            goto out;
        }  /* end if */

        /* set target obj type to return */
        link_info->trg_type = trg_oinfo.type;
        link_info->objno = trg_oinfo.addr;
        link_info->fileno = trg_oinfo.fileno;
    } /* end if */
    else
        link_info->trg_type = H5O_TYPE_UNKNOWN;

    /* succeed */
    ret = 1;

out:
    if(fapl != H5P_DEFAULT)
        H5Pclose(fapl);
    if(lapl != H5P_DEFAULT)
        H5Pclose(lapl);

    return ret;
} /* end H5tools_get_symlink_info() */
Exemplo n.º 20
0
/*-------------------------------------------------------------------------
 * Function:  process_cmpd_fields
 *
 * Purpose: To check whether the fields selected in "g_list_of_fields"
 *	    are valid fields associated with the dataset.
 *
 * Return: 0 on success; negative on failure
 *
 * Programmer:  Vailin Choi; August 2010
 *
 *-------------------------------------------------------------------------
 */
static herr_t
process_cmpd_fields(hid_t fid, char *dsetname)
{
    hid_t did=-1;			/* dataset id */
    hid_t dtid=-1, tid=-1;	/* dataset's data type id */
    size_t len;		/* number of comma-separated fields in "g_list_of_fields" */
    herr_t ret_value = SUCCEED;	/* Return value */

    HDassert(g_list_of_fields && *g_list_of_fields);
    
    /* Open the dataset */
    if((did = H5Dopen2(fid, dsetname, H5P_DEFAULT)) < 0) {
	error_msg("error in opening dataset \"%s\"\n", dsetname);
	ret_value = FAIL;
	goto done;
    }

    /* Get the dataset's datatype  */
    if(((dtid = H5Dget_type(did)) < 0) || (tid = H5Tget_native_type(dtid, H5T_DIR_DEFAULT)) < 0) {
	error_msg("error in getting dataset's datatype\n");
        ret_value = FAIL;
        goto done;
    }

    /* Check to make sure that the dataset's datatype is compound type */
    if(H5Tget_class(dtid) != H5T_COMPOUND) {
	error_msg("dataset should be compound type for <list_of_fields>\n");
	ret_value = FAIL;
	goto done;		
    }

    /* Make a copy of "g_list_of_fields" */
    if((g_dup_fields = HDstrdup(g_list_of_fields)) == NULL) {
	error_msg("error in duplicating g_list_of_fields\n");
        ret_value = FAIL;
	goto done;		
    }

    /* Estimate the number of comma-separated fields in "g_list of_fields" */
    len = HDstrlen(g_list_of_fields)/2 + 2;

    /* Allocate memory for a list vector of H5LD_memb_t structures to store "g_list_of_fields" info */
    if((g_listv = (H5LD_memb_t **)HDcalloc(len, sizeof(H5LD_memb_t *))) == NULL) {
	error_msg("error in allocating memory for H5LD_memb_t\n");
        ret_value = FAIL;
	goto done;		
    }

    /* Process and store info for "g_listv" */
    if(H5LD_construct_vector(g_dup_fields, g_listv, tid) < 0) {
	error_msg("error in processing <list_of_fields>\n");
	ret_value = FAIL;
        goto done;
    }

    /* Will free memory for g_listv and g_dup_fields when exiting from h5watch */
done:
    /* Closing */
    H5E_BEGIN_TRY
	H5Tclose(dtid);
	H5Tclose(tid);
	H5Dclose(did);
    H5E_END_TRY
    return(ret_value);
} /* process_cmpd_fields() */
Exemplo n.º 21
0
/*-------------------------------------------------------------------------
 * Function: H5LD_get_dset_type_size
 *
 * Purpose: To return the size of the dataset's datatype in bytes
 *	null "fields": return the size of the dataset's datatype
 *	non-null "fields": return the size of the dataset's datatype
 *			   with respect to the selection in "fields"
 *
 * Return: Success: size of the dataset's datatype
 *	   Failure: 0 (valid datatypes are never zero size)
 *
 * Programmer:  Vailin Choi; March 2010
 *
 *-------------------------------------------------------------------------
 */
static size_t
H5LD_get_dset_type_size(hid_t did, const char *fields)
{
    hid_t dset_tid = -1;	/* Dataset's type identifier */
    hid_t tid = -1;		/* Native Type identifier */
    H5LD_memb_t **listv = NULL;	/* Vector for storing information in "fields" */
    char *dup_fields = NULL;	/* A copy of "fields" */
    size_t ret_value = 0;       /* Return value */

    /* Get the datatype of the dataset */
    if((dset_tid = H5Dget_type(did)) < 0)
        goto done;
    if((tid = H5Tget_native_type(dset_tid, H5T_DIR_DEFAULT)) < 0)
        goto done;

    if(fields == NULL) /* If no "fields" is specified */
        ret_value = H5Tget_size(tid);
    else { /* "fields" are specified */
        size_t len;			/* Estimate the number of comma-separated fields in "fields" */
        size_t tot = 0;			/* Data type size of all the fields in "fields" */
        int n = 0, num = 0;		/* Local index variables */

        HDassert(fields && *fields);

        /* Should be a compound datatype if "fields" exists */
        if(H5Tget_class(dset_tid) != H5T_COMPOUND)
            goto done;

	/* Get a copy of "fields" */
        if(NULL == (dup_fields = HDstrdup(fields)))
            goto done;

	/* Allocate memory for a list of H5LD_memb_t pointers to store "fields" info */
	len = (HDstrlen(fields) / 2) + 2;
	if(NULL == (listv = (H5LD_memb_t **)HDcalloc(len, sizeof(H5LD_memb_t *))))
            goto done;

	/* Process and store info for "fields" */
	if((num = H5LD_construct_vector(dup_fields, listv/*OUT*/, tid)) < 0)
            goto done;

	/* Sum up the size of all the datatypes in "fields" */
	for(n = 0; n < num; n++)
	    tot += listv[n]->last_tsize;

	/* Clean up the vector of H5LD_memb_t structures */
	H5LD_clean_vector(listv);

	/* Return the total size */
	ret_value = tot;
    } /* end else */

done:
    H5E_BEGIN_TRY
        H5Tclose(tid);
        H5Tclose(dset_tid);
    H5E_END_TRY

    /* Free the array of H5LD_memb_t pointers */
    if(listv)
        HDfree(listv);

    /* Free memory */
    if(dup_fields)
        HDfree(dup_fields);

    return(ret_value);
} /* H5LD_get_dset_type_size() */
Exemplo n.º 22
0
/*-------------------------------------------------------------------------
 * Function:	main
 *
 * Purpose:	Split an hdf5 file
 *
 * Return:	Success:
 *
 *		Failure:
 *
 * Programmer:	Robb Matzke
 *              Wednesday, May 13, 1998
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
int
main (int argc, char *argv[])
{
    const char	*prog_name;		/*program name			*/
    size_t	blk_size=1024;		/*size of each I/O block	*/
    char	*buf=NULL;		/*I/O block buffer		*/
    size_t	n, i;			/*counters			*/
    ssize_t	nio;			/*I/O return value		*/
    int		argno=1;		/*program argument number	*/
    int		src, dst=-1;		/*source & destination files	*/
    int		need_seek=FALSE;	/*destination needs to seek?	*/
    int		need_write;		/*data needs to be written?	*/
    h5_stat_t sb;                       /*temporary file stat buffer	*/

    int		verbose=FALSE;		/*display file names?		*/

    const char	*src_gen_name;  /*general source name		*/
    char	*src_name=NULL;	    /*source member name		*/

    int		src_is_family;		/*is source name a family name?	*/
    int		src_membno=0;		/*source member number		*/

    const char	*dst_gen_name;	/*general destination name	*/
    char	*dst_name=NULL;	    /*destination member name	*/
    int		dst_is_family;		/*is dst name a family name?	*/
    int		dst_membno=0;		/*destination member number	*/

    off_t	left_overs=0;		/*amount of zeros left over	*/
    off_t	src_offset=0;		/*offset in source member	*/
    off_t	dst_offset=0;		/*offset in destination member	*/
    off_t	src_size;           /*source logical member size	*/
    off_t	src_act_size;		/*source actual member size	*/
    off_t	dst_size=1 GB;		/*destination logical memb size	*/
    hid_t       fapl;                   /*file access property list     */
    hid_t       file;
    hsize_t     hdsize;                 /*destination logical memb size */
    hbool_t     family_to_sec2=FALSE;   /*change family to sec2 driver? */

    /*
     * Get the program name from argv[0]. Use only the last component.
     */
    if ((prog_name=strrchr (argv[0], '/'))) prog_name++;
    else prog_name = argv[0];

    /*
     * Parse switches.
     */
    while (argno<argc && '-'==argv[argno][0]) {
        if (!strcmp (argv[argno], "-v")) {
            verbose = TRUE;
            argno++;
        } else if (!strcmp(argv[argno], "-V")) {
            printf("This is %s version %u.%u release %u\n",
                prog_name, H5_VERS_MAJOR, H5_VERS_MINOR, H5_VERS_RELEASE);
            exit(EXIT_SUCCESS);
        } else if (!strcmp (argv[argno], "-family_to_sec2")) {
            family_to_sec2 = TRUE;
            argno++;
        } else if ('b'==argv[argno][1]) {
            blk_size = (size_t)get_size (prog_name, &argno, argc, argv);
        } else if ('m'==argv[argno][1]) {
            dst_size = get_size (prog_name, &argno, argc, argv);
        } else {
            usage (prog_name);
        } /* end if */
    } /* end while */

    /* allocate names */
    if(NULL == (src_name = (char *)HDcalloc((size_t)NAMELEN, sizeof(char))))
        exit(EXIT_FAILURE);
    if(NULL == (dst_name = (char *)HDcalloc((size_t)NAMELEN, sizeof(char))))
        exit(EXIT_FAILURE);

    /*
     * Get the name for the source file and open the first member.  The size
     * of the first member determines the logical size of all the members.
     */
    if (argno>=argc) usage (prog_name);
    src_gen_name = argv[argno++];
    sprintf (src_name, src_gen_name, src_membno);
    src_is_family = strcmp (src_name, src_gen_name);

    if ((src = HDopen(src_name, O_RDONLY)) < 0) {
        HDperror(src_name);
        HDexit(EXIT_FAILURE);
    }

    if (HDfstat(src, &sb)<0) {
        perror ("fstat");
        exit (EXIT_FAILURE);
    }
    src_size = src_act_size = sb.st_size;
    if (verbose) fprintf (stderr, "< %s\n", src_name);

    /*
     * Get the name for the destination file and open the first member.
     */
    if (argno>=argc) usage (prog_name);
    dst_gen_name = argv[argno++];
    sprintf (dst_name, dst_gen_name, dst_membno);
    dst_is_family = strcmp (dst_name, dst_gen_name);

    if ((dst = HDopen(dst_name, O_RDWR|O_CREAT|O_TRUNC, H5_POSIX_CREATE_MODE_RW)) < 0) {
        HDperror(dst_name);
        HDexit(EXIT_FAILURE);
    }
    if (verbose) fprintf (stderr, "> %s\n", dst_name);

    /* No more arguments */
    if (argno<argc) usage (prog_name);

    /* Now the real work, split the file */
    buf = (char *)HDmalloc(blk_size);
    while (src_offset<src_size) {

	/* Read a block.  The amount to read is the minimum of:
	 *    1. The I/O block size
	 *    2. What's left to write in the destination member
	 *    3. Left over zeros or what's left in the source member.
	 */
	n = blk_size;
	if (dst_is_family) n = (size_t)MIN((off_t)n, dst_size-dst_offset);
	if (left_overs) {
	    n = (size_t)MIN ((off_t)n, left_overs);
	    left_overs = left_overs - (off_t)n;
	    need_write = FALSE;
	} else if (src_offset<src_act_size) {
	    n = (size_t)MIN ((off_t)n, src_act_size-src_offset);
	    if ((nio=HDread (src, buf, n))<0) {
		perror ("read");
		exit (EXIT_FAILURE);
	    } else if ((size_t)nio!=n) {
		fprintf (stderr, "%s: short read\n", src_name);
		exit (EXIT_FAILURE);
	    }
	    for (i=0; i<n; i++) {
		if (buf[i]) break;
	    }
	    need_write = (i<n);
	} else {
	    n = 0;
	    left_overs = src_size - src_act_size;
	    need_write = FALSE;
	}

	/*
	 * If the block contains non-zero data then write it to the
	 * destination, otherwise just remember that we'll have to do a seek
	 * later in the destination when we finally get non-zero data.
	 */
	if (need_write) {
	    if (need_seek && HDlseek (dst, dst_offset, SEEK_SET)<0) {
		perror ("HDlseek");
		exit (EXIT_FAILURE);
	    }
	    if ((nio=HDwrite (dst, buf, n))<0) {
		perror ("write");
		exit (EXIT_FAILURE);
	    } else if ((size_t)nio!=n) {
		fprintf (stderr, "%s: short write\n", dst_name);
		exit (EXIT_FAILURE);
	    }
	    need_seek = FALSE;
	} else {
	    need_seek = TRUE;
	}

	/*
	 * Update the source offset and open the next source family member if
	 * necessary.  The source stream ends at the first member which
	 * cannot be opened because it doesn't exist.  At the end of the
	 * source stream, update the destination offset and break out of the
	 * loop.   The destination offset must be updated so we can fix
	 * trailing holes.
	 */
	src_offset = src_offset + (off_t)n;
	if (src_offset==src_act_size) {
	    HDclose (src);
	    if (!src_is_family) {
                dst_offset = dst_offset + (off_t)n;
		break;
	    }
	    sprintf (src_name, src_gen_name, ++src_membno);
	    if ((src = HDopen(src_name, O_RDONLY)) < 0 && ENOENT == errno) {
            dst_offset = dst_offset + (off_t)n;
            break;
	    } else if (src<0) {
		perror (src_name);
		exit (EXIT_FAILURE);
	    }
	    if (HDfstat (src, &sb)<0) {
		perror ("fstat");
		exit (EXIT_FAILURE);
	    }
	    src_act_size = sb.st_size;
	    if (src_act_size>src_size) {
		fprintf (stderr, "%s: member truncated to %lu bytes\n",
			 src_name, (unsigned long)src_size);
	    }
	    src_offset = 0;
	    if (verbose) fprintf (stderr, "< %s\n", src_name);
	}

	/*
	 * Update the destination offset, opening a new member if one will be
	 * needed. The first member is extended to the logical member size
	 * but other members might be smaller if they end with a hole.
	 */
        dst_offset = dst_offset + (off_t)n;
	if (dst_is_family && dst_offset==dst_size) {
	    if (0==dst_membno) {
		if (HDlseek (dst, dst_size-1, SEEK_SET)<0) {
		    perror ("HDHDlseek");
		    exit (EXIT_FAILURE);
		}
		if (HDread (dst, buf, 1)<0) {
		    perror ("read");
		    exit (EXIT_FAILURE);
		}
		if (HDlseek (dst, dst_size-1, SEEK_SET)<0) {
		    perror ("HDlseek");
		    exit (EXIT_FAILURE);
		}
		if (HDwrite (dst, buf, 1)<0) {
		    perror ("write");
		    exit (EXIT_FAILURE);
		}
	    }
	    HDclose (dst);
	    sprintf (dst_name, dst_gen_name, ++dst_membno);
	    if ((dst = HDopen(dst_name, O_RDWR|O_CREAT|O_TRUNC, H5_POSIX_CREATE_MODE_RW)) < 0) {
            HDperror(dst_name);
            HDexit(EXIT_FAILURE);
	    }
	    dst_offset = 0;
	    need_seek = FALSE;
	    if (verbose) fprintf (stderr, "> %s\n", dst_name);
	}
    }

    /*
     * Make sure the last family member is the right size and then close it.
     * The last member can't end with a hole or hdf5 will think that the
     * family has been truncated.
     */
    if (need_seek) {
	if (HDlseek (dst, dst_offset-1, SEEK_SET)<0) {
	    perror ("HDlseek");
	    exit (EXIT_FAILURE);
	}
	if (HDread (dst, buf, 1)<0) {
	    perror ("read");
	    exit (EXIT_FAILURE);
	}
	if (HDlseek (dst, dst_offset-1, SEEK_SET)<0) {
	    perror ("HDlseek");
	    exit (EXIT_FAILURE);
	}
	if (HDwrite (dst, buf, 1)<0) {
	    perror ("write");
	    exit (EXIT_FAILURE);
	}
    }
    HDclose (dst);

    /* Modify family driver information saved in superblock through private property.
     * These private properties are for this tool only. */
    if ((fapl=H5Pcreate(H5P_FILE_ACCESS))<0) {
        perror ("H5Pcreate");
        exit (EXIT_FAILURE);
    }

    if(family_to_sec2) {
        /* The user wants to change file driver from family to sec2. Open the file
         * with sec2 driver.  This property signals the library to ignore the family
         * driver information saved in the superblock. */
        if(H5Pset(fapl, H5F_ACS_FAMILY_TO_SEC2_NAME, &family_to_sec2) < 0) {
            perror ("H5Pset");
            exit (EXIT_FAILURE);
        }
    } else {
        /* Modify family size saved in superblock through private property. It signals
         * library to save the new member size(specified in command line) in superblock.
         * This private property is for this tool only. */
        if(H5Pset_fapl_family(fapl, H5F_FAMILY_DEFAULT, H5P_DEFAULT) < 0) {
            perror ("H5Pset_fapl_family");
            exit (EXIT_FAILURE);
        }

        /* Set the property of the new member size as hsize_t */
        hdsize = (hsize_t)dst_size;
        if(H5Pset(fapl, H5F_ACS_FAMILY_NEWSIZE_NAME, &hdsize) < 0) {
            perror ("H5Pset");
            exit (EXIT_FAILURE);
        }
    }

    /* If the new file is a family file, try to open file for "read and write" to
     * flush metadata. Flushing metadata will update the superblock to the new
     * member size.  If the original file is a family file and the new file is a sec2
     * file, the property FAMILY_TO_SEC2 will signal the library to switch to sec2
     * driver when the new file is opened.  If the original file is a sec2 file and the
     * new file can only be a sec2 file, reopen the new file should fail.  There's
     * nothing to do in this case. */
    H5E_BEGIN_TRY {
        file=H5Fopen(dst_gen_name, H5F_ACC_RDWR, fapl);
    } H5E_END_TRY;

    if(file>=0) {
        if(H5Fclose(file)<0) {
            perror ("H5Fclose");
            exit (EXIT_FAILURE);
        } /* end if */
    } /* end if */

    if(H5Pclose(fapl)<0) {
        perror ("H5Pclose");
        exit (EXIT_FAILURE);
    } /* end if */

    /* Free resources and return */
    HDfree(src_name);
    HDfree(dst_name);
    HDfree(buf);
    return EXIT_SUCCESS;
} /* end main */
Exemplo n.º 23
0
/*-------------------------------------------------------------------------
 * Function:    test_logging_api
 *
 * Purpose:     Tests the API calls that affect mdc logging
 *
 * Return:      Success:        0
 *              Failure:        -1
 *-------------------------------------------------------------------------
 */
static herr_t
test_logging_api(void)
{
    hid_t       fapl = -1;
    hbool_t     is_enabled;
    hbool_t     is_enabled_out;
    hbool_t     start_on_access;
    hbool_t     start_on_access_out;
    char        *location = NULL;
    size_t      size;

    hid_t       fid;
    hid_t       gid;
    hbool_t     is_currently_logging;
    char        group_name[8];
    char        filename[1024];
    int         i;

    TESTING("metadata cache log api calls");

    fapl = h5_fileaccess();
    h5_fixname(FILE_NAME, fapl, filename, sizeof filename);

    /* Set up metadata cache logging */
    is_enabled = TRUE;
    start_on_access = FALSE;
    if(H5Pset_mdc_log_options(fapl, is_enabled, LOG_LOCATION, start_on_access) < 0)
        TEST_ERROR;

    /* Check to make sure that the property list getter returns the correct
     * location string buffer size;
     */
    is_enabled_out = FALSE;
    start_on_access_out = TRUE;
    location = NULL;
    size = 999;
    if(H5Pget_mdc_log_options(fapl, &is_enabled_out, location, &size, 
                              &start_on_access_out) < 0)
        TEST_ERROR;
    if(size != strlen(LOG_LOCATION) + 1)
        TEST_ERROR;

    /* Check to make sure that the property list getter works */
    if(NULL == (location = (char *)HDcalloc(size, sizeof(char))))
        TEST_ERROR;
    if(H5Pget_mdc_log_options(fapl, &is_enabled_out, location, &size, 
                              &start_on_access_out) < 0)
        TEST_ERROR;
    if((is_enabled != is_enabled_out)
        || (start_on_access != start_on_access_out)
        || HDstrcmp(LOG_LOCATION, location))
        TEST_ERROR;

    /* Create a file */
    if(H5Pset_libver_bounds(fapl, H5F_LIBVER_LATEST, H5F_LIBVER_LATEST) < 0)
        TEST_ERROR;
    if((fid = H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, fapl)) < 0)
        TEST_ERROR;
    if(H5Pclose(fapl) < 0)
        TEST_ERROR;


    /* Check to see if the logging flags were set correctly */
    is_enabled = FALSE;
    is_currently_logging = TRUE;
    if((H5Fget_mdc_logging_status(fid, &is_enabled, &is_currently_logging) < 0)
       || (is_enabled != TRUE)
       || (is_currently_logging != FALSE))
        TEST_ERROR;

    /* Turn on logging and check flags */
    if(H5Fstart_mdc_logging(fid) < 0)
        TEST_ERROR;
    is_enabled = FALSE;
    is_currently_logging = FALSE;
    if((H5Fget_mdc_logging_status(fid, &is_enabled, &is_currently_logging) < 0)
       || (is_enabled != TRUE)
       || (is_currently_logging != TRUE))
        TEST_ERROR;

    /* Perform some manipulations */
    for(i = 0; i < N_GROUPS; i++) {
        HDmemset(group_name, 0, 8);
        HDsnprintf(group_name, 8, "%d", i);
        if((gid = H5Gcreate2(fid, group_name, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0)
            TEST_ERROR;
        if(H5Gclose(gid) < 0)
            TEST_ERROR;
    }

    /* Turn off logging and check flags */
    if(H5Fstop_mdc_logging(fid) < 0)
        TEST_ERROR;
    is_enabled = FALSE;
    is_currently_logging = TRUE;
    if((H5Fget_mdc_logging_status(fid, &is_enabled, &is_currently_logging) < 0)
       || (is_enabled != TRUE)
       || (is_currently_logging != FALSE))
        TEST_ERROR;

    /* Clean up */
    HDfree(location);
    if(H5Fclose(fid) < 0)
        TEST_ERROR;

    PASSED();
    return 0;

error:
    return 1;
 } /* test_logging_api() */
Exemplo n.º 24
0
/****************************************************************
**
**  test_vlstrings_basic(): Test basic VL string code.
**      Tests simple VL string I/O
**
****************************************************************/
static void
test_vlstrings_basic(void)
{
    const char *wdata[SPACE1_DIM1]= {
        "Four score and seven years ago our forefathers brought forth on this continent a new nation,",
        "conceived in liberty and dedicated to the proposition that all men are created equal.",
        "Now we are engaged in a great civil war,",
        "testing whether that nation or any nation so conceived and so dedicated can long endure."
        };   /* Information to write */
    char *rdata[SPACE1_DIM1];   /* Information read in */
    char *wdata2;
    hid_t dataspace, dataset2;
    hid_t		fid1;		/* HDF5 File IDs		*/
    hid_t		dataset;	/* Dataset ID			*/
    hid_t		sid1;       /* Dataspace ID			*/
    hid_t		tid1;       /* Datatype ID			*/
    hid_t       xfer_pid;   /* Dataset transfer property list ID */
    hsize_t		dims1[] = {SPACE1_DIM1};
    hsize_t     size;       /* Number of bytes which will be used */
    unsigned       i;          /* counting variable */
    size_t         str_used;   /* String data in memory */
    size_t         mem_used=0; /* Memory used during allocation */
    herr_t		ret;		/* Generic return value		*/

    /* Output message about test being performed */
    MESSAGE(5, ("Testing Basic VL String Functionality\n"));

    /* Create file */
    fid1 = H5Fcreate(DATAFILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
    CHECK(fid1, FAIL, "H5Fcreate");

    /* Create dataspace for datasets */
    sid1 = H5Screate_simple(SPACE1_RANK, dims1, NULL);
    CHECK(sid1, FAIL, "H5Screate_simple");

    /* Create a datatype to refer to */
    tid1 = H5Tcopy(H5T_C_S1);
    CHECK(tid1, FAIL, "H5Tcopy");

    ret = H5Tset_size(tid1,H5T_VARIABLE);
    CHECK(ret, FAIL, "H5Tset_size");

    /* Create a dataset */
    dataset = H5Dcreate2(fid1, "Dataset1", tid1, sid1, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
    CHECK(dataset, FAIL, "H5Dcreate2");

    /* Write dataset to disk */
    ret = H5Dwrite(dataset, tid1, H5S_ALL, H5S_ALL, H5P_DEFAULT, wdata);
    CHECK(ret, FAIL, "H5Dwrite");

    dataspace = H5Screate(H5S_SCALAR);

    dataset2 = H5Dcreate2(fid1, "Dataset2", tid1, dataspace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
    CHECK(dataset, FAIL, "H5Dcreate2");

    wdata2 = (char*)HDcalloc((size_t)65534, sizeof(char));
    HDmemset(wdata2, 'A', (size_t)65533);

    ret = H5Dwrite(dataset2, tid1, H5S_ALL, H5S_ALL, H5P_DEFAULT, &wdata2);
    CHECK(ret, FAIL, "H5Dwrite");

    H5Sclose(dataspace);
    H5Dclose(dataset2);
    HDfree(wdata2);

    /* Change to the custom memory allocation routines for reading VL string */
    xfer_pid = H5Pcreate(H5P_DATASET_XFER);
    CHECK(xfer_pid, FAIL, "H5Pcreate");

    ret=H5Pset_vlen_mem_manager(xfer_pid,test_vlstr_alloc_custom,&mem_used,test_vlstr_free_custom,&mem_used);
    CHECK(ret, FAIL, "H5Pset_vlen_mem_manager");

    /* Make certain the correct amount of memory will be used */
    ret=H5Dvlen_get_buf_size(dataset,tid1,sid1,&size);
    CHECK(ret, FAIL, "H5Dvlen_get_buf_size");

    /* Count the actual number of bytes used by the strings */
    for(i=0,str_used=0; i<SPACE1_DIM1; i++)
        str_used+=HDstrlen(wdata[i])+1;

    /* Compare against the strings actually written */
    VERIFY(size,(hsize_t)str_used,"H5Dvlen_get_buf_size");

    /* Read dataset from disk */
    ret = H5Dread(dataset, tid1, H5S_ALL, H5S_ALL, xfer_pid, rdata);
    CHECK(ret, FAIL, "H5Dread");

    /* Make certain the correct amount of memory has been used */
    VERIFY(mem_used,str_used,"H5Dread");

    /* Compare data read in */
    for(i = 0; i < SPACE1_DIM1; i++) {
        if(HDstrlen(wdata[i]) != HDstrlen(rdata[i])) {
            TestErrPrintf("VL data length don't match!, strlen(wdata[%d])=%d, strlen(rdata[%d])=%d\n",(int)i,(int)strlen(wdata[i]),(int)i,(int)strlen(rdata[i]));
            continue;
        } /* end if */
        if(HDstrcmp(wdata[i], rdata[i]) != 0 ) {
            TestErrPrintf("VL data values don't match!, wdata[%d]=%s, rdata[%d]=%s\n",(int)i,wdata[i],(int)i,rdata[i]);
            continue;
        } /* end if */
    } /* end for */

    /* Reclaim the read VL data */
    ret = H5Dvlen_reclaim(tid1,sid1,xfer_pid,rdata);
    CHECK(ret, FAIL, "H5Dvlen_reclaim");

    /* Make certain the VL memory has been freed */
    VERIFY(mem_used,0,"H5Dvlen_reclaim");

    /* Close Dataset */
    ret = H5Dclose(dataset);
    CHECK(ret, FAIL, "H5Dclose");

    /* Close datatype */
    ret = H5Tclose(tid1);
    CHECK(ret, FAIL, "H5Tclose");

    /* Close disk dataspace */
    ret = H5Sclose(sid1);
    CHECK(ret, FAIL, "H5Sclose");

    /* Close dataset transfer property list */
    ret = H5Pclose(xfer_pid);
    CHECK(ret, FAIL, "H5Pclose");

    /* Close file */
    ret = H5Fclose(fid1);
    CHECK(ret, FAIL, "H5Fclose");

} /* end test_vlstrings_basic() */
Exemplo n.º 25
0
    /* Create the file with/without latest format: ensure version 2 object header for SWMR */
    if(new_format)  {
        /* Set to use latest library format */
        if(H5Pset_libver_bounds(fapl, H5F_LIBVER_LATEST, H5F_LIBVER_LATEST) < 0)
            FAIL_STACK_ERROR

        if((fid = H5Fcreate(FILE_OHDR_SWMR, H5F_ACC_TRUNC, H5P_DEFAULT, fapl)) < 0)
            TEST_ERROR
    } /* end if */
    else {
        if((fid = H5Fcreate(FILE_OHDR_SWMR, H5F_ACC_TRUNC|H5F_ACC_SWMR_WRITE, H5P_DEFAULT, fapl)) < 0)
            TEST_ERROR
    } /* end else */

    /* Initialize data */
    wbuf = (int *)HDcalloc(compact_size, sizeof(int));
    n = 0;
    for(u = 0; u < compact_size; u++)
        wbuf[u] = n++;

    /* Create a small data space for compact dataset */
    dims[0] = (hsize_t)compact_size;
    if((sid = H5Screate_simple(1, dims, NULL)) < 0)
        FAIL_STACK_ERROR

    /* Create property list for compact dataset creation */
    if((plist = H5Pcreate(H5P_DATASET_CREATE)) < 0)
        FAIL_STACK_ERROR

    /* Set the layout for the compact dataset */
    if(H5Pset_layout(plist, H5D_COMPACT) < 0)
Exemplo n.º 26
0
/*-------------------------------------------------------------------------
 * Function:	H5S_obtain datatype
 *
 * Purpose:	Obtain an MPI derived datatype based on span-tree
                implementation
 *
 * Return:	non-negative on success, negative on failure.
 *
 * Outputs:	*span_type	 the MPI type corresponding to the selection
 *
 * Programmer:  kyang
 *
 */
static herr_t H5S_obtain_datatype(const hsize_t size[],
			      H5S_hyper_span_t* span,
			      MPI_Datatype *span_type,
			      size_t elmt_size,
			      int dimindex)
{

  int                   innercount,outercount;
  MPI_Datatype          bas_type;
  MPI_Datatype          temp_type;
  MPI_Datatype          tempinner_type;
  MPI_Datatype          *inner_type;
  int                   *blocklen;
  MPI_Aint              *disp;
  MPI_Aint              stride;
  MPI_Aint              extent,lb;
  H5S_hyper_span_info_t *down;
  H5S_hyper_span_t      *tspan;
  int                   mpi_code;
  herr_t                ret_value = SUCCEED;

#ifdef H5_HAVE_MPI2
  MPI_Aint              sizeaint,sizedtype;
#endif /* H5_HAVE_MPI2 */
  hsize_t               total_lowd,total_lowd1;
  int                   i;
  int                   ret;

  FUNC_ENTER_NOAPI_NOINIT(H5S_obtain_datatype);
  assert(span);

  inner_type = NULL;
  down       = NULL;
  tspan      = NULL;
  down       = span->down;
  tspan      = span;

  outercount = 0;

/* obtain the number of span tree for this dimension */
  while(tspan) {
    tspan = tspan->next;
    outercount ++;
  }

  if(outercount == 0) {
    span_type = NULL;
    return 0;
  }

/* MPI2 hasn't been widely acccepted, adding H5_HAVE_MPI2 for the future use */
#ifdef H5_HAVE_MPI2

  MPI_Type_extent(MPI_Aint,&sizeaint);
  MPI_Type_extent(MPI_Datatype,&sizedtype);

  blocklen  = (int *)HDcalloc((size_t)outercount,sizeof(int));
  disp = (MPI_Aint *)HDcalloc((size_t)outercount,sizeaint);
  inner_type   = (MPI_Datatype *)HDcalloc((size_t)outercount,sizedtype);

#else

  blocklen     = (int *)HDcalloc((size_t)outercount,sizeof(int));
  disp         = (MPI_Aint *)HDcalloc((size_t)outercount,sizeof(MPI_Aint));
  inner_type   = (MPI_Datatype *)HDcalloc((size_t)outercount,sizeof(MPI_Datatype));

#endif


  tspan      = span;
  outercount = 0;

  /* if this is the fastest changing dimension, it is the base case for derived datatype. */
  if(down == NULL){

    assert(dimindex <= 1);
    if(MPI_SUCCESS != (mpi_code = MPI_Type_contiguous((int)elmt_size, MPI_BYTE,&bas_type)))
          HMPI_GOTO_ERROR(FAIL, "MPI_Type_contiguous failed", mpi_code);

    if(MPI_SUCCESS != (mpi_code = MPI_Type_commit(&bas_type)))
          HMPI_GOTO_ERROR(FAIL, "MPI_Type_commit failed", mpi_code);

    while(tspan){

      disp[outercount]      = (MPI_Aint)elmt_size * tspan->low;
      blocklen[outercount]  = tspan->nelem;
      tspan                 = tspan->next;
      outercount ++;
    }

    if(MPI_SUCCESS != (mpi_code = MPI_Type_hindexed(outercount,blocklen,
						    disp,bas_type,span_type)))
          HMPI_GOTO_ERROR(FAIL, "MPI_Type_hindexed failed", mpi_code);

  }
  else {/* dimindex is the rank of the dimension */

    assert(dimindex >1);
    /* Calculate the total bytes of the lower dimension */
    total_lowd  = 1;  /* one dimension down */
    total_lowd1 = 1; /* two dimensions down */

    for ( i = dimindex-1; i > 0; i--)
         total_lowd = total_lowd * size[i];

    for ( i = dimindex-1; i > 1; i--)
          total_lowd1 = total_lowd1 * size[i];

     while(tspan){

       /* Displacement should be in byte and should have dimension information */
       /* First using MPI Type vector to build derived data type for this span only */
       /* Need to calculate the disp in byte for this dimension. */
       /* Calculate the total bytes of the lower dimension */

       disp[outercount]      = tspan->low*total_lowd*elmt_size;
       blocklen[outercount]  = 1;

       /* generating inner derived datatype by using MPI_Type_hvector */
       if(FAIL == H5S_obtain_datatype(size,tspan->down->head,&temp_type,elmt_size,dimindex-1))
          HGOTO_ERROR(H5E_DATASPACE, H5E_BADTYPE, FAIL,"couldn't obtain  MPI derived data type");

       if(MPI_SUCCESS != (mpi_code = MPI_Type_commit(&temp_type)))
          HMPI_GOTO_ERROR(FAIL, "MPI_Type_commit failed", mpi_code);

       /* building the inner vector datatype */
       stride     = total_lowd*elmt_size;
       innercount = tspan->nelem;

       if(MPI_SUCCESS != (mpi_code = MPI_Type_hvector(innercount,1,stride,temp_type,&tempinner_type)))
          HMPI_GOTO_ERROR(FAIL, "MPI_Type_hvector failed", mpi_code);

       if(MPI_SUCCESS != (mpi_code = MPI_Type_commit(&tempinner_type)))
          HMPI_GOTO_ERROR(FAIL, "MPI_Type_commit failed", mpi_code);

       if(MPI_SUCCESS != (mpi_code =MPI_Type_free(&temp_type)))
          HMPI_GOTO_ERROR(FAIL, "MPI_Type_free failed",mpi_code);
       inner_type[outercount] = tempinner_type;
       outercount ++;
       tspan = tspan->next;

     }

    /* building the whole vector datatype */
    if(MPI_SUCCESS != (mpi_code =
		       MPI_Type_struct(outercount,blocklen,disp,inner_type,span_type)))
          HMPI_GOTO_ERROR(FAIL, "MPI_Type_struct failed", mpi_code);

  }

  if(inner_type != NULL){
    if(down != NULL) {
        for(i=0;i<outercount;i++)
             if(MPI_SUCCESS != (mpi_code = MPI_Type_free(&inner_type[i])))
                HMPI_GOTO_ERROR(FAIL, "MPI_Type_free failed",mpi_code);
    }
  }

  if(inner_type != NULL)
    HDfree(inner_type);
  if(blocklen != NULL)
    HDfree(blocklen);
  if(disp != NULL)
    HDfree(disp);
done:
  FUNC_LEAVE_NOAPI(ret_value);
}
Exemplo n.º 27
0
/*-------------------------------------------------------------------------
 * Function: H5LD_construct_vector
 *
 * Purpose: Process the comma-separated list of fields in "fields" as follows:
 * 	Example:
 *		"fields": "a.b.c,d"
 *		listv[0]->tot_offset = total offset of "a" & "b" & "c"
 *		listv[0]->last_tid = type id of "c"
 *		listv[0]->last_tsize = type size of "c"
 *		listv[0]->names[0] = "a"
 *		listv[0]->names[1] = "b"
 *		listv[0]->names[2] = "c"
 *		listv[0]->names[3] = NULL
 *
 *		listv[1]->tot_offset = offset of "d"
 *		listv[1]->last_tid = type id of "d"
 *		listv[1]->last_tsize = type size of "d"
 *		listv[1]->names[0] = "d"
 *		listv[1]->names[1] = NULL
 *
 * Return: Success: # of comma-separated fields in "fields"
 *	   Failure: negative value
 *
 * Programmer:  Vailin Choi; Aug 2010
 *
*-------------------------------------------------------------------------
*/
int
H5LD_construct_vector(char *fields, H5LD_memb_t *listv[]/*OUT*/, hid_t par_tid)
{
    int nfields;	                /* The # of comma-separated fields in "fields" */
    hbool_t end_of_fields = FALSE;	/* end of "fields" */
    char *fields_ptr;	                /* Pointer to "fields" */
    int ret_value = FAIL;		/* Return value */

    HDassert(listv);
    HDassert(fields);

    fields_ptr = fields;
    nfields = 0;

    /* Process till end of "fields" */
    while(!end_of_fields) {
	H5LD_memb_t *memb = NULL; 	/* Pointer to structure for storing a field's info */
        char *cur;	                /* Pointer to a member in a field */
	size_t len;			/* Estimated # of members in a field */
	hbool_t gotcomma = FALSE;	/* A comma encountered */
	hbool_t gotmember = FALSE;	/* Getting member in a field */
        hbool_t valid = TRUE;           /* Whether a field being processed is valid or not */
	int j = 0;			/* The # of members in a field */

	len = (HDstrlen(fields_ptr) / 2) + 2;

	/* Allocate memory for an H5LD_memb_t for storing a field's info */
	if(NULL == (memb = (H5LD_memb_t *)HDcalloc((size_t)1, sizeof(H5LD_memb_t))))
	    goto done;

	/* Allocate memory for an array of pointers to member names */
	if(NULL == (memb->names = (char **)HDcalloc(len, sizeof(char *))))
	    goto done;

	memb->names[j] = fields_ptr;
	memb->last_tid = -1;
	cur = fields_ptr;

	/* Continue processing till: not valid or comma encountered or "fields" ended */
	while(valid && !gotcomma && !end_of_fields) {
	    switch(*fields_ptr) {
                case '\0':	/* end of list */
                    if(gotmember) { /* getting something and end of "fields" */
                        *cur++ = '\0';;
                        memb->names[++j] = NULL;
                    } /* end if */
                    else /* getting nothing but end of list */
                        valid = FALSE;
                    end_of_fields = TRUE;
                    break;
	
                case '\\': /* escape character */
                    ++fields_ptr; /* skip it */
                    if(*fields_ptr == '\0')
                        valid = FALSE;
                    else {
                        *cur++ = *fields_ptr++;
                        gotmember = TRUE;
                    } /* end else */
                    break;

                case '.': /* nested field separator */
                    *fields_ptr++ = *cur++ = '\0';;
                    if(gotmember) {
                        memb->names[++j] = cur;
                        gotmember = FALSE;
                    } /* end if */
                    else
                        valid = FALSE;
                    break;

                case ',': /* field separator */
                    *fields_ptr++ = *cur++ = '\0';;
                    if(gotmember) {
                        memb->names[++j] = NULL;
                        gotmember = FALSE;
                    } /* end if */
                    else
                        valid = FALSE;
                    gotcomma = TRUE;
                    break;

                default: 
                    *cur++ = *fields_ptr++;
                    gotmember = TRUE;
                    break;
	    } /* end switch */
	} /* while (valid && !gotcomma && !end_of_fields) */

	/* If valid, put into listv and continue processing further info */
	if(valid) {
	    listv[nfields++] = memb;
	    if(H5LD_construct_info(memb, par_tid) < 0)
		goto done;
	} /* end if */
        else {
	    if(memb)
                HDfree(memb);
	    goto done;
	} /* end else */
    } /* while !end_of_fields */

    /* Indicate success */
    ret_value = nfields;

done:
    listv[nfields] = NULL;
    if(ret_value == FAIL)
	H5LD_clean_vector(listv);

    return(ret_value);
}  /* H5LD_construct_vector() */
Exemplo n.º 28
0
int do_copy_refobjs(hid_t fidin,
                    hid_t fidout,
                    trav_table_t *travt,
                    pack_opt_t *options) /* repack options */
{
    hid_t     grp_in=(-1);            /* read group ID */
    hid_t     grp_out=(-1);           /* write group ID */
    hid_t     dset_in=(-1);           /* read dataset ID */
    hid_t     dset_out=(-1);          /* write dataset ID */
    hid_t     type_in=(-1);           /* named type ID */
    hid_t     dcpl_id=(-1);           /* dataset creation property list ID */
    hid_t     space_id=(-1);          /* space ID */
    hid_t     ftype_id=(-1);          /* file data type ID */
    hid_t     mtype_id=(-1);          /* memory data type ID */
    size_t    msize;                  /* memory size of memory type */
    hsize_t   nelmts;                 /* number of elements in dataset */
    int       rank;                   /* rank of dataset */
    hsize_t   dims[H5S_MAX_RANK];     /* dimensions of dataset */
    unsigned int i, j;
    int       k;
    
    /*-------------------------------------------------------------------------
    * browse
    *-------------------------------------------------------------------------
    */
    
    for ( i = 0; i < travt->nobjs; i++)
    {
        switch ( travt->objs[i].type )
        {
        /*-------------------------------------------------------------------------
        * H5G_GROUP
        *-------------------------------------------------------------------------
        */
        case H5G_GROUP:
            
        /*-------------------------------------------------------------------------
        * copy referenced objects in attributes
        *-------------------------------------------------------------------------
        */
            
            if ((grp_out=H5Gopen(fidout,travt->objs[i].name))<0)
                goto error;
            if((grp_in = H5Gopen (fidin,travt->objs[i].name))<0)
                goto error;
            if (copy_refs_attr(grp_in,grp_out,options,travt,fidout)<0)
                goto error;
            if (H5Gclose(grp_out)<0)
                goto error;
            if (H5Gclose(grp_in)<0)
                goto error;
            
           /*-------------------------------------------------------------------------
            * check for hard links
            *-------------------------------------------------------------------------
            */
            
            if (travt->objs[i].nlinks)
            {
                for ( j=0; j<travt->objs[i].nlinks; j++)
                {
                    H5Glink(fidout,
                        H5G_LINK_HARD,
                        travt->objs[i].name,
                        travt->objs[i].links[j].new_name);
                }
            }
            
            break;
            
       /*-------------------------------------------------------------------------
       * H5G_DATASET
       *-------------------------------------------------------------------------
       */
        case H5G_DATASET:
            
            if ((dset_in=H5Dopen(fidin,travt->objs[i].name))<0)
                goto error;
            if ((space_id=H5Dget_space(dset_in))<0)
                goto error;
            if ((ftype_id=H5Dget_type (dset_in))<0)
                goto error;
            if ((dcpl_id=H5Dget_create_plist(dset_in))<0)
                goto error;
            if ( (rank=H5Sget_simple_extent_ndims(space_id))<0)
                goto error;
            if ( H5Sget_simple_extent_dims(space_id,dims,NULL)<0)
                goto error;
            nelmts=1;
            for (k=0; k<rank; k++)
                nelmts*=dims[k];
            
            if ((mtype_id=h5tools_get_native_type(ftype_id))<0)
                goto error;
            
            if ((msize=H5Tget_size(mtype_id))==0)
                goto error;
            
            
           /*-------------------------------------------------------------------------
           * check if the dataset creation property list has filters that
           * are not registered in the current configuration
           * 1) the external filters GZIP and SZIP might not be available
           * 2) the internal filters might be turned off
           *-------------------------------------------------------------------------
           */
            if (h5tools_canreadf((NULL),dcpl_id)==1)
            {
               /*-------------------------------------------------------------------------
                * test for a valid output dataset
                *-------------------------------------------------------------------------
                */
                dset_out = FAIL;
                
                /*-------------------------------------------------------------------------
                * object references are a special case
                * we cannot just copy the buffers, but instead we recreate the reference
                *-------------------------------------------------------------------------
                */
                if (H5Tequal(mtype_id, H5T_STD_REF_OBJ))
                {
                    H5G_obj_t1       obj_type;
                    hid_t            refobj_id;
                    hobj_ref_t       *refbuf=NULL; /* buffer for object references */
                    hobj_ref_t       *buf=NULL;
                    const char*      refname;
                    unsigned         u;
                    
                    /*-------------------------------------------------------------------------
                    * read to memory
                    *-------------------------------------------------------------------------
                    */
                    
                    if (nelmts)
                    {
                        buf=(void *) HDmalloc((unsigned)(nelmts*msize));
                        if ( buf==NULL){
                            printf( "cannot read into memory\n" );
                            goto error;
                        }
                        if (H5Dread(dset_in,mtype_id,H5S_ALL,H5S_ALL,H5P_DEFAULT,buf)<0)
                            goto error;
                        
                        if ((obj_type = H5Rget_obj_type(dset_in,H5R_OBJECT,buf))<0)
                            goto error;
                        refbuf=HDcalloc((unsigned)nelmts,msize);
                        if ( refbuf==NULL){
                            printf( "cannot allocate memory\n" );
                            goto error;
                        }
                        for ( u=0; u<nelmts; u++)
                        {
                            H5E_BEGIN_TRY {
                                if ((refobj_id = H5Rdereference(dset_in,H5R_OBJECT,&buf[u]))<0)
                                    continue;
                            } H5E_END_TRY;
                            /* get the name. a valid name could only occur in the
                            second traversal of the file */
                            if ((refname=MapIdToName(refobj_id,travt))!=NULL)
                            {
                                /* create the reference, -1 parameter for objects */
                                if (H5Rcreate(&refbuf[u],fidout,refname,H5R_OBJECT,-1)<0)
                                    goto error;
                                if(options->verbose)
                                {
                                    
                                    
                                    printf(FORMAT_OBJ,"dset",travt->objs[i].name );
                                    printf("object <%s> object reference created to <%s>\n",
                                        travt->objs[i].name,
                                        refname);
                                }
                            }/*refname*/
                            close_obj(obj_type,refobj_id);
                        }/*  u */
                    }/*nelmts*/
                    
                     /*-------------------------------------------------------------------------
                     * create/write dataset/close
                     *-------------------------------------------------------------------------
                    */
                    if ((dset_out=H5Dcreate(fidout,travt->objs[i].name,mtype_id,space_id,dcpl_id))<0)
                        goto error;
                    if (nelmts) {
                        if (H5Dwrite(dset_out,mtype_id,H5S_ALL,H5S_ALL,H5P_DEFAULT,refbuf)<0)
                            goto error;
                    }
                    
                    if (buf)
                        free(buf);
                    if (refbuf)
                        free(refbuf);
                    
                }/*H5T_STD_REF_OBJ*/
                
                 /*-------------------------------------------------------------------------
                 * dataset region references
                 *-------------------------------------------------------------------------
                */
                else if (H5Tequal(mtype_id, H5T_STD_REF_DSETREG))
                {
                    H5G_obj_t1       obj_type;
                    hid_t            refobj_id;
                    hdset_reg_ref_t  *refbuf=NULL; /* input buffer for region references */
                    hdset_reg_ref_t  *buf=NULL;    /* output buffer */
                    const char*      refname;
                    unsigned         u;
                    
                    /*-------------------------------------------------------------------------
                    * read input to memory
                    *-------------------------------------------------------------------------
                    */
                    if (nelmts)
                    {
                        buf=(void *) HDmalloc((unsigned)(nelmts*msize));
                        if ( buf==NULL){
                            printf( "cannot read into memory\n" );
                            goto error;
                        }
                        if (H5Dread(dset_in,mtype_id,H5S_ALL,H5S_ALL,H5P_DEFAULT,buf)<0)
                            goto error;
                        if ((obj_type = H5Rget_obj_type(dset_in,H5R_DATASET_REGION,buf))<0)
                            goto error;
                        
                        /*-------------------------------------------------------------------------
                        * create output
                        *-------------------------------------------------------------------------
                        */
                        
                        refbuf=HDcalloc(sizeof(hdset_reg_ref_t),(size_t)nelmts); /*init to zero */
                        if ( refbuf==NULL){
                            printf( "cannot allocate memory\n" );
                            goto error;
                        }
                        for ( u=0; u<nelmts; u++)
                        {
                            H5E_BEGIN_TRY {
                                if ((refobj_id = H5Rdereference(dset_in,H5R_DATASET_REGION,&buf[u]))<0)
                                    continue;
                            } H5E_END_TRY;
                            
                            /* get the name. a valid name could only occur in the
                            second traversal of the file */
                            if ((refname=MapIdToName(refobj_id,travt))!=NULL)
                            {
                                hid_t region_id;    /* region id of the referenced dataset */
                                if ((region_id = H5Rget_region(dset_in,H5R_DATASET_REGION,&buf[u]))<0)
                                    goto error;
                                /* create the reference, we need the space_id */
                                if (H5Rcreate(&refbuf[u],fidout,refname,H5R_DATASET_REGION,region_id)<0)
                                    goto error;
                                if (H5Sclose(region_id)<0)
                                    goto error;
                                if(options->verbose)
                                {
                                    
                                    
                                    
                                    printf(FORMAT_OBJ,"dset",travt->objs[i].name );
                                    printf("object <%s> region reference created to <%s>\n",
                                        travt->objs[i].name,
                                        refname);
                                }
                            }/*refname*/
                            close_obj(obj_type,refobj_id);
                        }/*  u */
                    }/*nelmts*/
                    
                     /*-------------------------------------------------------------------------
                     * create/write dataset/close
                     *-------------------------------------------------------------------------
                    */
                    if ((dset_out=H5Dcreate(fidout,travt->objs[i].name,mtype_id,space_id,dcpl_id))<0)
                        goto error;
                    if (nelmts) {
                        if (H5Dwrite(dset_out,mtype_id,H5S_ALL,H5S_ALL,H5P_DEFAULT,refbuf)<0)
                            goto error;
                    }
                    
                    if (buf)
                        free(buf);
                    if (refbuf)
                        free(refbuf);
                } /* H5T_STD_REF_DSETREG */
                
                
               /*-------------------------------------------------------------------------
                * not references, open previously created object in 1st traversal
                *-------------------------------------------------------------------------
                */
                else
                {
                    if ((dset_out=H5Dopen(fidout,travt->objs[i].name))<0)
                        goto error;
                }
                
                assert(dset_out!=FAIL);
                
                /*-------------------------------------------------------------------------
                * copy referenced objects in attributes
                *-------------------------------------------------------------------------
                */
                if (copy_refs_attr(dset_in,dset_out,options,travt,fidout)<0)
                    goto error;
                
                
                /*-------------------------------------------------------------------------
                * check for hard links
                *-------------------------------------------------------------------------
                */
                if (travt->objs[i].nlinks)
                {
                    for ( j=0; j<travt->objs[i].nlinks; j++){
                        H5Glink(fidout,
                            H5G_LINK_HARD,
                            travt->objs[i].name,
                            travt->objs[i].links[j].new_name);
                    }
                }
                
                if (H5Dclose(dset_out)<0)
                    goto error;
                
   }/*can_read*/
Exemplo n.º 29
0
/*-------------------------------------------------------------------------
 * Function: H5LD_get_dset_elmts
 *
 * Purpose: To retrieve selected data from the dataset
 *
 * Return: Success: 0
 *	   Failure: negative
 *
 * Programmer:  Vailin Choi; August 2010
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5LD_get_dset_elmts(hid_t did, const hsize_t *prev_dims, const hsize_t *cur_dims,
    const char *fields, void *buf)
{
    hid_t dtid = -1, tid = -1;	/* Dataset type id */
    hid_t sid = -1, mid = -1;	/* Dataspace and memory space id */
    hssize_t snum_elmts;        /* Number of dataset elements in the selection (signed) */
    hsize_t num_elmts;          /* Number of dataset elements in the selection */
    hsize_t start[H5S_MAX_RANK];/* Starting offset */
    hsize_t count[H5S_MAX_RANK];/* ??offset */
    H5LD_memb_t **listv = NULL;	/* Vector for storing information in "fields" */
    char *dup_fields = NULL;	/* A copy of "fields" */
    char *sav_buf = NULL;	/* Saved pointer temporary buffer */
    unsigned ctr;	        /* Counter for # of curr_dims > prev_dims */
    int ndims;		        /* Number of dimensions for the dataset */
    int i; 		        /* Local index variable */
    herr_t ret_value = FAIL;	/* Return value */

    /* Verify parameters */
    if(prev_dims == NULL || cur_dims == NULL || buf == NULL)
	goto done;

    /* Get dataset's dataspace */
    if((sid = H5Dget_space(did)) < 0)
	goto done;
    
    /* Get the number of dimensions */
    if((ndims = H5Sget_simple_extent_ndims(sid)) < 0)
	goto done;

    /* Verify that cur_dims must have one dimension whose size is greater than prev_dims */
    HDmemset(start, 0, sizeof start);
    HDmemset(count, 0, sizeof count);
    ctr = 0;
    for(i = 0; i < ndims; i++)
	if(cur_dims[i] > prev_dims[i]) {
	    ++ctr;
	    count[i] = cur_dims[i] - prev_dims[i];
	    start[i] = prev_dims[i];
	} /* end if */
        else { /* < or = */
	    start[i] = 0;
	    count[i] = MIN(prev_dims[i], cur_dims[i]);
	} /* end else */
    if(!ctr)
	goto done;

    if(ctr == 1) { /* changes for only one dimension */
	/* Make the selection in the dataset based on "cur_dims" and "prev_dims" */
	if(H5Sselect_hyperslab(sid, H5S_SELECT_SET, start, NULL, count, NULL) < 0)
	    goto done;
    } /* end if */
    else { /* changes for more than one dimensions */
	HDmemset(start, 0, sizeof start);

	/* Make the selection in the dataset based on "cur_dims" and "prev_dims" */
	if(H5Sselect_hyperslab(sid, H5S_SELECT_SET, start, NULL, cur_dims, NULL) < 0)
	    goto done;
	if(H5Sselect_hyperslab(sid, H5S_SELECT_NOTB, start, NULL, prev_dims, NULL) < 0)
	    goto done;
    } /* end else */

    /* Get the number of elements in the selection */
    if(0 == (snum_elmts = H5Sget_select_npoints(sid)))
        goto done;
    num_elmts = (hsize_t)snum_elmts;

    /* Create the memory space for the selection */
    if((mid = H5Screate_simple(1, &num_elmts, NULL)) < 0)
	goto done;

    /* Get the native datatype size */
    if((dtid = H5Dget_type(did)) < 0)
        goto done;
    if((tid = H5Tget_native_type(dtid, H5T_DIR_DEFAULT)) < 0)
        goto done;

    if(fields == NULL) { /* nothing in "fields" */
	/* Read and store all the elements in "buf" */
	if(H5Dread(did, tid, mid, sid, H5P_DEFAULT, buf) < 0)
	    goto done;
    } /* end if */
    else { /* "fields" is specified */
	unsigned char *buf_p = (unsigned char *)buf;   /* Pointer to the destination buffer */
        char *tmp_buf;          /* Temporary buffer for data read */
        size_t tot_tsize;	/* Total datatype size */
        size_t len;		/* Estimate the number of comma-separated fields in "fields" */

	/* should be a compound datatype if "fields" exists */
	if(H5Tget_class(tid) != H5T_COMPOUND)
	    goto done;

	/* Get the total size of the dataset's datatypes */
	if(0 == (tot_tsize = H5LD_get_dset_type_size(did, NULL)))
	    goto done;
	
	/* Allocate memory for reading in the elements in the dataset selection */
	if(NULL == (sav_buf = tmp_buf = (char *)HDcalloc((size_t)num_elmts, tot_tsize)))
	    goto done;

	/* Read the dataset elements in the selection */
	if(H5Dread(did, tid, mid, sid, H5P_DEFAULT, tmp_buf) < 0)
	    goto done;

	/* Make a copy of "fields" */
	if(NULL == (dup_fields = HDstrdup(fields)))
	    goto done;

	/* Allocate memory for the vector of H5LD_memb_t pointers */
	len = (HDstrlen(fields) / 2) + 2;
        if(NULL == (listv = (H5LD_memb_t **)HDcalloc(len, sizeof(H5LD_memb_t *))))
	    goto done;

	/* Process and store information for "fields" */
        if(H5LD_construct_vector(dup_fields, listv, tid) < 0)
            goto done;

	/* Copy data for each dataset element in the selection */
	for(i = 0; i < (int)num_elmts; i++) {
            int j; 		/* Local index variable */

	    /* Copy data for "fields" to the input buffer */
	    for(j = 0; listv[j] != NULL; j++) {
		HDmemcpy(buf_p, tmp_buf + listv[j]->tot_offset, listv[j]->last_tsize);
		buf_p += listv[j]->last_tsize;
	    } /* end for */
	    tmp_buf += tot_tsize;
	} /* end for */

	/* Clean up the vector of H5LD_memb_t structures */
	H5LD_clean_vector(listv);
    } /* end else */

    /* Indicate success */
    ret_value = SUCCEED;

done:
    H5E_BEGIN_TRY
	H5Tclose(dtid);
	H5Tclose(tid);
	H5Sclose(sid);
	H5Sclose(mid);
    H5E_END_TRY

    /* Free the array of H5LD_memb_t pointers */
    if(listv)
        HDfree(listv);

    /* Free memory */
    if(dup_fields)
        HDfree(dup_fields);
    if(sav_buf)
        HDfree(sav_buf);

    return(ret_value);
} /* H5LD_get_dset_elmts() */