Пример #1
0
    /**
     * Internal use only!
     */
    void add(DomainData* entry)
    {
        if (entry == NULL)
            throw DCException("Entry in DataContainer must not be NULL.");

        if (entry->getData() == NULL)
            throw DCException("Data in entry in DataContainer must not be NULL.");

        const Dimensions &entryOffset = entry->getOffset();
        const Dimensions entryBack = entry->getBack(); // last index INSIDE

        for (uint32_t i = 0; i < 3; ++i)
        {
            offset[i] = std::min(entryOffset[i], offset[i]);
            size[i] = std::max(entryBack[i] + 1 - offset[i], size[i]);
        }

        subdomains.push_back(entry);
    }
Пример #2
0
    H5Handle DCGroup::create(H5Handle base, std::string path)
    throw (DCException)
    {
        bool mustCreate = false;
        H5Handle currentHandle = base;
        char c_path[path.size() + 1];
        strcpy(c_path, path.c_str());

        char *token = strtok(c_path, "/");
        while (token)
        {
            if (mustCreate || !H5Lexists(currentHandle, token, H5P_DEFAULT))
            {
                H5Handle newHandle = H5Gcreate(currentHandle, token, H5P_LINK_CREATE_DEFAULT,
                        H5P_GROUP_CREATE_DEFAULT, H5P_GROUP_ACCESS_DEFAULT);
                if (newHandle < 0)
                    throw DCException(getExceptionString("Failed to create group", path));

                currentHandle = newHandle;
                mustCreate = true;
            } else
            {
                currentHandle = H5Gopen(currentHandle, token, H5P_DEFAULT);
                if (currentHandle < 0)
                {
                    if (checkExistence)
                        throw DCException(getExceptionString("Failed to create group", path));
                    
                    currentHandle = H5Gcreate(currentHandle, token, H5P_LINK_CREATE_DEFAULT,
                        H5P_GROUP_CREATE_DEFAULT, H5P_GROUP_ACCESS_DEFAULT);
                    if (currentHandle < 0)
                        throw DCException(getExceptionString("Failed to create group", path));
                    
                    mustCreate = true;
                }
            }

            handles.push_back(currentHandle);
            token = strtok(NULL, "/");
        }

        return currentHandle;
    }
Пример #3
0
 void DCDataSet::setCompression()
 throw (DCException)
 {
     if (this->compression && getPhysicalSize().getScalarSize() != 0)
     {
         // shuffling reorders bytes for better compression
         // set gzip compression level (1=lowest - 9=highest)
         if (H5Pset_shuffle(this->dsetProperties) < 0 ||
                 H5Pset_deflate(this->dsetProperties, 1) < 0)
             throw DCException(getExceptionString("setCompression: Failed to set compression"));
     }
 }
Пример #4
0
    void DCGroup::close()
    throw (DCException)
    {
        for (HandlesList::const_reverse_iterator iter = handles.rbegin();
                iter != handles.rend(); ++iter)
        {
            if (H5Gclose(*iter) < 0)
                throw DCException(getExceptionString("Failed to close group", ""));
        }

        handles.clear();
    }
Пример #5
0
    void DCDataSet::createReference(hid_t refGroup,
            hid_t srcGroup,
            DCDataSet &srcDataSet)
    throw (DCException)
    {
        if (opened)
            throw DCException(getExceptionString("createReference: dataset is already open"));

        if (checkExistence && H5Lexists(refGroup, name.c_str(), H5P_LINK_ACCESS_DEFAULT))
            throw DCException(getExceptionString("createReference: this reference already exists"));

        getLogicalSize().set(srcDataSet.getLogicalSize());
        this->ndims = srcDataSet.getNDims();

        if (H5Rcreate(&regionRef, srcGroup, srcDataSet.getName().c_str(), H5R_OBJECT, -1) < 0)
            throw DCException(getExceptionString("createReference: failed to create region reference"));

        hsize_t ndims = 1;
        dataspace = H5Screate_simple(1, &ndims, NULL);
        if (dataspace < 0)
            throw DCException(getExceptionString("createReference: failed to create dataspace for reference"));

        dataset = H5Dcreate(refGroup, name.c_str(), H5T_STD_REF_OBJ,
                dataspace, H5P_DEFAULT, dsetProperties, H5P_DEFAULT);

        if (dataset < 0)
            throw DCException(getExceptionString("createReference: failed to create dataset for reference"));

        if (H5Dwrite(dataset, H5T_STD_REF_OBJ, H5S_ALL, H5S_ALL,
                dsetWriteProperties, &regionRef) < 0)
            throw DCException(getExceptionString("createReference: failed to write reference"));

        isReference = true;
        opened = true;
    }
Пример #6
0
 void DCAttribute::readAttribute(const char* name, hid_t parent, void* dst)
 throw (DCException)
 {
     hid_t attr = H5Aopen(parent, name, H5P_DEFAULT);
     if (attr < 0)
         throw DCException(getExceptionString(name, "Attribute could not be opened for reading"));
     
     hid_t attr_type = H5Aget_type(attr);
     if (attr_type < 0)
     {
         H5Aclose(attr);
         throw DCException(getExceptionString(name, "Could not get type of attribute"));
     }
     
     if (H5Aread(attr, attr_type, dst) < 0)
     {
         H5Aclose(attr);
         throw DCException(getExceptionString(name, "Attribute could not be read"));
     }
     
     H5Aclose(attr);
 }
Пример #7
0
    void DCAttribute::writeAttribute(const char* name, const hid_t type, hid_t parent, const void* src)
    throw (DCException)
    {
        hid_t attr = -1;
        if (H5Aexists(parent, name))
            attr = H5Aopen(parent, name, H5P_DEFAULT);
        else
        {
            hid_t dsp = H5Screate(H5S_SCALAR);
            attr = H5Acreate(parent, name, type, dsp, H5P_DEFAULT, H5P_DEFAULT);
            H5Sclose(dsp);
        }

        if (attr < 0)
            throw DCException(getExceptionString(name, "Attribute could not be opened or created"));

        if (H5Awrite(attr, type, src) < 0)
        {
            H5Aclose(attr);
            throw DCException(getExceptionString(name, "Attribute could not be written"));
        }

        H5Aclose(attr);
    }
Пример #8
0
    void DCDataSet::createReference(hid_t refGroup,
            hid_t srcGroup,
            DCDataSet &srcDataSet,
            Dimensions count,
            Dimensions offset,
            Dimensions stride)
    throw (DCException)
    {
        if (opened)
            throw DCException(getExceptionString("createReference: dataset is already open"));

        if (checkExistence && H5Lexists(refGroup, name.c_str(), H5P_LINK_ACCESS_DEFAULT))
            throw DCException(getExceptionString("createReference: this reference already exists"));

        getLogicalSize().set(count);
        this->ndims = srcDataSet.getNDims();

        count.swapDims(this->ndims);
        offset.swapDims(this->ndims);
        stride.swapDims(this->ndims);

        // select region hyperslab in source dataset
        if (H5Sselect_hyperslab(srcDataSet.getDataSpace(), H5S_SELECT_SET,
                offset.getPointer(), stride.getPointer(),
                count.getPointer(), NULL) < 0 ||
                H5Sselect_valid(srcDataSet.getDataSpace()) <= 0)
            throw DCException(getExceptionString("createReference: failed to select hyperslap for reference"));

        if (H5Rcreate(&regionRef, srcGroup, srcDataSet.getName().c_str(), H5R_DATASET_REGION,
                srcDataSet.getDataSpace()) < 0)
            throw DCException(getExceptionString("createReference: failed to create region reference"));

        hsize_t ndims = 1;
        dataspace = H5Screate_simple(1, &ndims, NULL);
        if (dataspace < 0)
            throw DCException(getExceptionString("createReference: failed to create dataspace for reference"));

        dataset = H5Dcreate(refGroup, name.c_str(), H5T_STD_REF_DSETREG,
                dataspace, H5P_DEFAULT, dsetProperties, H5P_DEFAULT);

        if (dataset < 0)
            throw DCException(getExceptionString("createReference: failed to create dataset for reference"));

        if (H5Dwrite(dataset, H5T_STD_REF_DSETREG, H5S_ALL, H5S_ALL,
                dsetWriteProperties, &regionRef) < 0)
            throw DCException(getExceptionString("createReference: failed to write reference"));

        isReference = true;
        opened = true;
    }
Пример #9
0
    void DCDataSet::setChunking(size_t typeSize)
    throw (DCException)
    {
        if (getPhysicalSize().getScalarSize() != 0)
        {
            // get chunking dimensions
            hsize_t chunk_dims[ndims];
            DCHelper::getOptimalChunkDims(getPhysicalSize().getPointer(), ndims,
                    typeSize, chunk_dims);

            if (H5Pset_chunk(this->dsetProperties, ndims, chunk_dims) < 0)
            {
                for (size_t i = 0; i < ndims; ++i)
                {
                    log_msg(1, "chunk_dims[%llu] = %llu",
                            (long long unsigned) i, (long long unsigned) (chunk_dims[i]));
                }
                throw DCException(getExceptionString("setChunking: Failed to set chunking"));
            }
        }
    }
Пример #10
0
    DCDataType DCDataSet::getDCDataType() throw (DCException)
    {
        if (!opened)
            throw DCException(getExceptionString("getDCDataType: dataset is not opened"));

        DCDataType result = DCDT_UNKNOWN;

        H5T_class_t type_class = H5Tget_class(datatype);
        size_t type_size = H5Tget_size(datatype);
        H5T_sign_t type_signed = H5Tget_sign(datatype);

        if (type_class == H5T_INTEGER)
        {
            if (type_signed == H5T_SGN_NONE)
            {
                if (type_size == sizeof (uint64_t))
                    result = DCDT_UINT64;
                else
                    result = DCDT_UINT32;
            } else
            {
                if (type_size == sizeof (int64_t))
                    result = DCDT_INT64;
                else
                    result = DCDT_INT32;
            }
        } else
            if (type_class == H5T_FLOAT)
        {
            // float or double
            if (type_size == sizeof (float))
                result = DCDT_FLOAT32;
            else
                if (type_size == sizeof (double))
                result = DCDT_FLOAT64;
        }

        return result;
    }
Пример #11
0
    void DCDataSet::write(Dimensions srcBuffer, Dimensions srcStride,
            Dimensions srcOffset, Dimensions srcData,
            Dimensions dstOffset, const void* data)
    throw (DCException)
    {
        log_msg(2, "DCDataSet::write (%s)", name.c_str());

        if (!opened)
            throw DCException(getExceptionString("write: Dataset has not been opened/created"));

        log_msg(3,
                " ndims = %llu\n"
                " logical_size = %s\n"
                " physical_size = %s\n"
                " src_buffer = %s\n"
                " src_stride = %s\n"
                " src_data = %s\n"
                " src_offset = %s\n"
                " dst_offset = %s\n",
                (long long unsigned) ndims,
                getLogicalSize().toString().c_str(),
                getPhysicalSize().toString().c_str(),
                srcBuffer.toString().c_str(),
                srcStride.toString().c_str(),
                srcData.toString().c_str(),
                srcOffset.toString().c_str(),
                dstOffset.toString().c_str());

        // swap dimensions if necessary
        srcBuffer.swapDims(ndims);
        srcStride.swapDims(ndims);
        srcData.swapDims(ndims);
        srcOffset.swapDims(ndims);
        dstOffset.swapDims(ndims);

        // dataspace to read from
        hid_t dsp_src;

        if (getLogicalSize().getScalarSize() != 0)
        {
            dsp_src = H5Screate_simple(ndims, srcBuffer.getPointer(), NULL);
            if (dsp_src < 0)
                throw DCException(getExceptionString("write: Failed to create source dataspace"));

            if (H5Sselect_hyperslab(dsp_src, H5S_SELECT_SET, srcOffset.getPointer(),
                    srcStride.getPointer(), srcData.getPointer(), NULL) < 0 ||
                    H5Sselect_valid(dsp_src) <= 0)
                throw DCException(getExceptionString("write: Invalid source hyperslap selection"));

            if (srcData.getScalarSize() == 0)
                H5Sselect_none(dsp_src);

            // dataspace to write to
            if (H5Sselect_hyperslab(dataspace, H5S_SELECT_SET, dstOffset.getPointer(),
                    NULL, srcData.getPointer(), NULL) < 0 ||
                    H5Sselect_valid(dataspace) <= 0)
                throw DCException(getExceptionString("write: Invalid target hyperslap selection"));

            if (!data || (srcData.getScalarSize() == 0))
            {
                H5Sselect_none(dataspace);
                data = NULL;
            }

            // write data to the dataset

            if (H5Dwrite(dataset, this->datatype, dsp_src, dataspace, dsetWriteProperties, data) < 0)
                throw DCException(getExceptionString("write: Failed to write dataset"));

            H5Sclose(dsp_src);
        }
    }
Пример #12
0
    void DCDataSet::read(Dimensions dstBuffer,
            Dimensions dstOffset,
            Dimensions srcSize,
            Dimensions srcOffset,
            Dimensions& sizeRead,
            uint32_t& srcNDims,
            void* dst)
    throw (DCException)
    {
        log_msg(2, "DCDataSet::read (%s)", name.c_str());

        if (!opened)
            throw DCException(getExceptionString("read: Dataset has not been opened/created"));

        if (dstBuffer.getScalarSize() == 0)
            dstBuffer.set(srcSize);

        // dst buffer is allowed to be NULL
        // in this case, only the size of the dataset is returned
        // if the dataset is empty, return just its size as there is nothing to read
        if ((dst != NULL) && (getNDims() > 0))
        {
            log_msg(3,
                    " ndims = %llu\n"
                    " logical_size = %s\n"
                    " physical_size = %s\n"
                    " dstBuffer = %s\n"
                    " dstOffset = %s\n"
                    " srcSize = %s\n"
                    " srcOffset = %s\n",
                    (long long unsigned) ndims,
                    getLogicalSize().toString().c_str(),
                    getPhysicalSize().toString().c_str(),
                    dstBuffer.toString().c_str(),
                    dstOffset.toString().c_str(),
                    srcSize.toString().c_str(),
                    srcOffset.toString().c_str());

            dstBuffer.swapDims(ndims);
            dstOffset.swapDims(ndims);
            srcSize.swapDims(ndims);
            srcOffset.swapDims(ndims);

            hid_t dst_dataspace = H5Screate_simple(ndims, dstBuffer.getPointer(), NULL);
            if (dst_dataspace < 0)
                throw DCException(getExceptionString("read: Failed to create target dataspace"));

            if (H5Sselect_hyperslab(dst_dataspace, H5S_SELECT_SET, dstOffset.getPointer(), NULL,
                    srcSize.getPointer(), NULL) < 0 ||
                    H5Sselect_valid(dst_dataspace) <= 0)
                throw DCException(getExceptionString("read: Target dataspace hyperslab selection is not valid!"));

            if (H5Sselect_hyperslab(dataspace, H5S_SELECT_SET, srcOffset.getPointer(), NULL,
                    srcSize.getPointer(), NULL) < 0 ||
                    H5Sselect_valid(dataspace) <= 0)
                throw DCException(getExceptionString("read: Source dataspace hyperslab selection is not valid!"));

            if (srcSize.getScalarSize() == 0)
                H5Sselect_none(dataspace);
            
            if (H5Dread(dataset, this->datatype, dst_dataspace, dataspace, dsetReadProperties, dst) < 0)
                throw DCException(getExceptionString("read: Failed to read dataset"));

            H5Sclose(dst_dataspace);

            srcSize.swapDims(ndims);
        }

        // swap dimensions if necessary
        sizeRead.set(srcSize);
        srcNDims = this->ndims;

        log_msg(3, " returns ndims = %llu", (long long unsigned) ndims);
        log_msg(3, " returns sizeRead = %s", sizeRead.toString().c_str());
    }
Пример #13
0
 void DCGroup::remove(H5Handle base, std::string path)
 throw (DCException)
 {
     if (H5Ldelete(base, path.c_str(), H5P_LINK_ACCESS_DEFAULT) < 0)
         throw DCException(getExceptionString("failed to remove group", path));
 }
Пример #14
0
void ToolsSplashParallel::convertToText()
{
    if (m_options.data.size() == 0)
        throw std::runtime_error("No datasets requested");

    ColTypeInt ctInt;
    ColTypeDouble ctDouble;

    // read data
    //

    std::vector<ExDataContainer> file_data;

    // identify reference data class (poly, grid), reference domain size and number of elements
    //

    DomainCollector::DomDataClass ref_data_class = DomainCollector::UndefinedType;
    try
    {
        dc.readAttribute(m_options.step, m_options.data[0].c_str(), DOMCOL_ATTR_CLASS,
                &ref_data_class, NULL);
    } catch (const DCException&)
    {
        errorStream << "Error: No domain information for dataset '" << m_options.data[0] << "' available." << std::endl;
        errorStream << "This might not be a valid libSplash domain." << std::endl;
        return;
    }

    switch (ref_data_class)
    {
        case DomainCollector::GridType:
            if (m_options.verbose)
                errorStream << "Converting GRID data" << std::endl;
            break;
        case DomainCollector::PolyType:
            if (m_options.verbose)
                errorStream << "Converting POLY data" << std::endl;
            break;
        default:
            throw std::runtime_error("Could not identify data class for requested dataset");
    }

    Domain ref_total_domain;
    ref_total_domain = dc.getGlobalDomain(m_options.step, m_options.data[0].c_str());

    for (std::vector<std::string>::const_iterator iter = m_options.data.begin();
            iter != m_options.data.end(); ++iter)
    {
        // check that all datasets match to each other
        //

        DomainCollector::DomDataClass data_class = DomainCollector::UndefinedType;
        dc.readAttribute(m_options.step, iter->c_str(), DOMCOL_ATTR_CLASS,
                &data_class, NULL);
        if (data_class != ref_data_class)
            throw std::runtime_error("All requested datasets must be of the same data class");

        Domain total_domain = dc.getGlobalDomain(m_options.step, iter->c_str());
        if (total_domain != ref_total_domain)
            throw std::runtime_error("All requested datasets must map to the same domain");

        // create an extended container for each dataset
        ExDataContainer excontainer;

        if (ref_data_class == DomainCollector::PolyType)
        {
            // poly type

            excontainer.container = dc.readDomain(m_options.step, iter->c_str(),
                    Domain(total_domain.getOffset(), total_domain.getSize()), NULL, true);
        } else
        {
            // grid type

            Dimensions offset(total_domain.getOffset());
            Dimensions domain_size(total_domain.getSize());

            for (int i = 0; i < 3; ++i)
                if (m_options.fieldDims[i] == 0)
                {
                    offset[i] = m_options.sliceOffset;
                    if (offset[i] > total_domain.getBack()[i])
                        throw DCException("Requested offset outside of domain");

                    domain_size[i] = 1;
                    break;
                }

            excontainer.container = dc.readDomain(m_options.step, iter->c_str(),
                    Domain(offset, domain_size), NULL);
        }

        // read unit
        //
        if (m_options.applyUnits)
        {
            try
            {
                dc.readAttribute(m_options.step, iter->c_str(), "unitSI",
                        &(excontainer.unit), NULL);
            } catch (const DCException&)
            {
                if (m_options.verbose)
                    errorStream << "no unit for '" << iter->c_str() << "', defaulting to 1.0" << std::endl;
                excontainer.unit = 1.0;
            }

            if (m_options.verbose)
                errorStream << "Loaded dataset '" << iter->c_str() << "' with unit '" <<
                excontainer.unit << "'" << std::endl;
        } else
        {
            excontainer.unit = 1.0;
            if (m_options.verbose)
                errorStream << "Loaded dataset '" << iter->c_str() << "'" << std::endl;
        }

        file_data.push_back(excontainer);
    }

    assert(file_data[0].container->get(0)->getData() != NULL);

    // write to file
    //
    if (ref_data_class == DomainCollector::PolyType)
        printParticles(file_data);
    else
        printFields(file_data);

    for (std::vector<ExDataContainer>::iterator iter = file_data.begin();
            iter != file_data.end(); ++iter)
    {
        delete iter->container;
    }
}