Skip to content

Commit cddce6b

Browse files
committed
Merge pull request #3 from alan-wu/master
Improve cmake script, hdf5; read from memory buffer
2 parents 48a9377 + 043338c commit cddce6b

19 files changed

+1856
-62
lines changed

CMakeLists.txt

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ CMAKE_MINIMUM_REQUIRED( VERSION 2.6 )
4141

4242
# Set this nampspace to keep these options seperate from other project options
4343
SET( FIELDML_NAMESPACE_NAME FIELDML )
44+
PROJECT(fieldml)
4445

4546
MACRO( OPTION_WITH_DEFAULT OPTION_NAME OPTION_STRING OPTION_DEFAULT )
4647
IF( NOT DEFINED ${OPTION_NAME} )
@@ -102,9 +103,17 @@ ADD_SUBDIRECTORY( core )
102103
SET( LIBRARY_TARGET_NAME fieldml_io_api )
103104
SET( FIELDML_IO_API_LIBRARY_TARGET_NAME ${LIBRARY_TARGET_NAME} )
104105
ADD_SUBDIRECTORY( io )
105-
106+
GET_DIRECTORY_PROPERTY(HDF5_INCLUDE_DIRS DIRECTORY io DEFINITION HDF5_INCLUDE_DIRS)
107+
GET_DIRECTORY_PROPERTY(HDF5_MINE_LIBRARIES DIRECTORY io DEFINITION HDF5_MINE_LIBRARIES)
108+
GET_DIRECTORY_PROPERTY(MPI_LIBRARIES DIRECTORY io DEFINITION MPI_MINE_LIBRARIES)
109+
GET_DIRECTORY_PROPERTY(MPI_INCLUDE_DIRS DIRECTORY io DEFINITION MPI_INCLUDE_DIRS)
110+
GET_DIRECTORY_PROPERTY(HDF5_USE_MPI DIRECTORY io DEFINITION HDF5_USE_MPI)
106111
ADD_SUBDIRECTORY( test )
107112

113+
foreach(arg ${MPI_LIBRARIES})
114+
set(MPI_MINE_LIBRARIES "${MPI_MINE_LIBRARIES} ${arg}")
115+
endforeach(arg ${MPI_LIBRARIES})
116+
108117
IF( ${FIELDML_NAMESPACE_NAME}_INSTALL_CONFIG )
109118
SET( LIBRARY_CONFIG_FILE ${CMAKE_CURRENT_BINARY_DIR}/fieldml-config.cmake )
110119
SET( CONFIG_FILE_CONTENTS
@@ -113,8 +122,8 @@ IF( ${FIELDML_NAMESPACE_NAME}_INSTALL_CONFIG )
113122
"\nGET_FILENAME_COMPONENT( SELF_DIR \"\${CMAKE_CURRENT_LIST_FILE}\" PATH )"
114123
"\nINCLUDE( \${SELF_DIR}/fieldml-targets.cmake )"
115124
"\nGET_FILENAME_COMPONENT( ${FIELDML_NAMESPACE_NAME}_INCLUDE_DIRS \"\${SELF_DIR}/../../include\" ABSOLUTE )"
116-
"\nSET( ${FIELDML_NAMESPACE_NAME}_INCLUDE_DIRS \"\${${FIELDML_NAMESPACE_NAME}_INCLUDE_DIRS}\" \"${LIBXML2_INCLUDE_DIR}\" \"${HDF5_INCLUDE_DIR}\" )"
117-
"\nSET( ${FIELDML_NAMESPACE_NAME}_LIBRARIES ${FIELDML_API_LIBRARY_TARGET_NAME} ${FIELDML_IO_API_LIBRARY_TARGET_NAME} )"
125+
"\nSET( ${FIELDML_NAMESPACE_NAME}_INCLUDE_DIRS \"\${${FIELDML_NAMESPACE_NAME}_INCLUDE_DIRS}\" \"${LIBXML2_INCLUDE_DIR}\" \"${HDF5_INCLUDE_DIRS}\" \"${MPI_INCLUDE_DIRS}\" )"
126+
"\nSET( ${FIELDML_NAMESPACE_NAME}_LIBRARIES ${FIELDML_API_LIBRARY_TARGET_NAME} ${FIELDML_IO_API_LIBRARY_TARGET_NAME} ${HDF5_MINE_LIBRARIES} ${MPI_MINE_LIBRARIES})"
118127
"\nSET( ${FIELDML_NAMESPACE_NAME}_DEFINITIONS )"
119128
"\nSET( ${FIELDML_NAMESPACE_NAME}_FOUND TRUE )"
120129
"\nENDIF( NOT DEFINED _${FIELDML_NAMESPACE_NAME}_CONFIG_CMAKE )"

Fortran/InterfaceGen.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ def processLine( line, state ):
203203
return state
204204

205205

206-
def processFile():
206+
def processFiles():
207207
headerFile = open( "../core/src/fieldml_api.h" )
208208

209209
state = ParseState.InHeader
@@ -235,6 +235,37 @@ def processFile():
235235

236236
headerFile.close()
237237

238+
headerFile = open( "../io/src/FieldmlIoApi.h" )
239+
240+
state = ParseState.InHeader
241+
inComment = False
242+
243+
for line in headerFile:
244+
startSegment = 0
245+
246+
if( inComment ):
247+
pos = line.find( "*/" )
248+
if( pos == -1 ):
249+
continue
250+
inComment = False
251+
startSegment = pos + 2
252+
253+
#Does not cope with mixing line and block comments.
254+
endSegment = line.find( "/*", startSegment )
255+
while( endSegment != -1 ):
256+
state = processLine( line[ startSegment : endSegment ], state )
257+
startSegment = line.find( "*/", endSegment + 2 )
258+
if( startSegment == -1 ):
259+
break
260+
endSegment = line.find( "/*", startSegment )
261+
262+
if( startSegment == -1 ):
263+
inComment = True
264+
else:
265+
state = processLine( line[ startSegment : ], state )
266+
267+
headerFile.close()
268+
238269

239270
writeHeader()
240271

@@ -261,4 +292,4 @@ def processFile():
261292

262293
writeFooter()
263294

264-
processFile()
295+
processFiles()

core/src/FieldmlSession.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,46 @@ FieldmlRegion *FieldmlSession::addResourceRegion( string href, string name )
200200
return resourceRegion;
201201
}
202202

203+
FieldmlRegion *FieldmlSession::addResourceRegionFromBuffer( const void *buffer, unsigned int buffer_length, string name )
204+
{
205+
if((buffer == 0) || (1 > buffer_length))
206+
{
207+
return 0;
208+
}
209+
210+
//NOTE: This will be insufficient when the region name starts being used.
211+
if( FmlUtil::contains( importHrefStack, name ) )
212+
{
213+
addError( "Recursive import involving " + name );
214+
return NULL;
215+
}
216+
217+
importHrefStack.push_back( name );
218+
219+
FieldmlRegion *resourceRegion = new FieldmlRegion( name, name, "", objects );
220+
FieldmlRegion *currentRegion = region;
221+
region = resourceRegion;
222+
223+
int result = 0;
224+
225+
result = FieldmlDOM::parseFieldmlString( (const char *)buffer, "memory buffer", "memory", this, getSessionHandle() );
226+
227+
importHrefStack.pop_back();
228+
229+
region = currentRegion;
230+
231+
if( ( result != 0 ) || ( getErrorCount() != 0 ) )
232+
{
233+
delete resourceRegion;
234+
resourceRegion = NULL;
235+
}
236+
else
237+
{
238+
regions.push_back( resourceRegion );
239+
}
240+
241+
return resourceRegion;
242+
}
203243

204244
void FieldmlSession::pushErrorContext( const char *file, const int line, const char *function )
205245
{

core/src/FieldmlSession.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ class FieldmlSession :
117117

118118
FieldmlRegion *addResourceRegion( std::string location, std::string name );
119119

120+
FieldmlRegion *addResourceRegionFromBuffer( const void *buffer, unsigned int buffer_length, std::string name );
121+
120122
FieldmlRegion *addNewRegion( std::string location, std::string name );
121123

122124
FieldmlRegion *getRegion( std::string location, std::string name );

core/src/fieldml_api.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,31 @@ FmlSessionHandle Fieldml_CreateFromFile( const char * filename )
514514
return session->getSessionHandle();
515515
}
516516

517+
FmlSessionHandle Fieldml_CreateFromBuffer( const void *buffer, unsigned int buffer_length, const char * name )
518+
{
519+
FieldmlSession *session = new FieldmlSession();
520+
ErrorContextAutostack bob( session, __FILE__, __LINE__, __ECA_FUNC__ );
521+
522+
if( buffer == NULL || 1 > buffer_length )
523+
{
524+
session->setError( FML_ERR_INVALID_PARAMETER_1, "Cannot create FieldML session. Invalid buffer." );
525+
}
526+
else
527+
{
528+
session->region = session->addResourceRegionFromBuffer( buffer, buffer_length, name );
529+
if( session->region == NULL )
530+
{
531+
session->setError( FML_ERR_READ_ERR, "Cannot create FieldML session. Invalid document or read error." );
532+
}
533+
else
534+
{
535+
session->region->finalize();
536+
}
537+
}
538+
539+
return session->getSessionHandle();
540+
}
541+
517542

518543
FmlSessionHandle Fieldml_Create( const char * location, const char * name )
519544
{

core/src/fieldml_api.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,8 @@ extern "C" {
273273
*/
274274
FmlSessionHandle Fieldml_CreateFromFile( const char * filename );
275275

276+
FmlSessionHandle Fieldml_CreateFromBuffer( const void *buffer, unsigned int buffer_length, const char * name );
277+
276278

277279
/**
278280
* Creates an empty FieldML handle.

io/CMakeLists.txt

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,23 +38,21 @@
3838
PROJECT( io )
3939

4040
OPTION_WITH_DEFAULT( FIELDML_USE_HDF5 "Do you want to use netgen?" FALSE )
41-
41+
SET( HDF5_USE_MPI FALSE )
42+
SET( HDF5_INCLUDE_DIRS "" )
43+
SET( HDF5_MINE_LIBRARIES "" )
44+
SET( MPI_INCLUDE_DIRS "" )
45+
SET( MPI_MINE_LIBRARIES "" )
46+
4247
IF( FIELDML_USE_HDF5 )
43-
FIND_PATH(HDF5_INCLUDE_DIR hdf5.h
44-
NO_DEFAULT_PATH
45-
)
46-
FIND_LIBRARY(HDF5_LIBRARY NAMES hdf5
47-
NO_DEFAULT_PATH
48-
)
49-
MARK_AS_ADVANCED(HDF5_INCLUDE_DIR)
50-
MARK_AS_ADVANCED(HDF5_LIBRARY)
48+
FIND_PACKAGE( HDF5 REQUIRED C )
49+
SET( HDF5_USE_MPI ${HDF5_ENABLE_PARALLEL} )
50+
IF ( HDF5_USE_MPI )
51+
FIND_PACKAGE( MPI REQUIRED )
52+
ENDIF (HDF5_USE_MPI)
5153
ADD_DEFINITIONS( -DFIELDML_HDF5_ARRAY )
52-
SET( HDF5_INCLUDE_DIRS ${HDF5_INCLUDE_DIR} )
53-
SET( HDF5_LIBRARIES ${HDF5_LIBRARY} )
54-
SET( CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} ${IELDML_HDF5_ARRAY_DEFINE} )
55-
SET( CMAKE ${CMAKE_C_FLAGS} ${IELDML_HDF5_ARRAY_DEFINE} )
56-
SET( DEPENDENT_LIBS ${DEPENDENT_LIBS} ${HDF5_LIBRARIES} )
57-
SET( INCLUDE_DIRS ${INCLUDE_DIRS} ${HDF5_INCLUDE_DIRS} )
54+
SET( CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} ${FIELDML_HDF5_ARRAY_DEFINE} )
55+
SET( CMAKE ${CMAKE_C_FLAGS} ${FIELDML_HDF5_ARRAY_DEFINE} )
5856
ENDIF( FIELDML_USE_HDF5 )
5957

6058
IF( ${FIELDML_NAMESPACE_NAME}_BUILD_STATIC_LIB )
@@ -68,6 +66,30 @@ ELSE( ${FIELDML_NAMESPACE_NAME}_BUILD_STATIC_LIB )
6866
ENDIF( WIN32 )
6967
ENDIF( ${FIELDML_NAMESPACE_NAME}_BUILD_STATIC_LIB )
7068

69+
IF( FIELDML_USE_HDF5 )
70+
IF( HDF5_FOUND )
71+
SET( HDF5_INCLUDE_DIRS ${HDF5_INCLUDE_DIR} )
72+
SET( HDF5_MINE_LIBRARIES ${HDF5_LIBRARIES} )
73+
SET( DEPENDENT_LIBS ${DEPENDENT_LIBS} ${HDF5_MINE_LIBRARIES} )
74+
SET( INCLUDE_DIRS ${INCLUDE_DIRS} ${HDF5_INCLUDE_DIRS} )
75+
IF( HDF5_USE_MPI )
76+
IF( MPI_FOUND )
77+
SET( MPI_INCLUDE_DIRS ${MPI_INCLUDE_PATH} )
78+
SET( MPI_MINE_LIBRARIES ${MPI_LIBRARIES} )
79+
SET( DEPENDENT_LIBS ${DEPENDENT_LIBS} ${MPI_MINE_LIBRARIES} )
80+
SET( INCLUDE_DIRS ${INCLUDE_DIRS} ${MPI_INCLUDE_DIRS} )
81+
ELSE( MPI_FOUND )
82+
MESSAGE( FATAL_ERROR "MPI was requested but not found." )
83+
ENDIF( MPI_FOUND )
84+
ADD_DEFINITIONS( -DFIELDML_PHDF5_ARRAY )
85+
SET( CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} ${FIELDML_PHDF5_ARRAY_DEFINE} )
86+
SET( CMAKE ${CMAKE_C_FLAGS} ${FIELDML_PHDF5_ARRAY_DEFINE} )
87+
ENDIF( HDF5_USE_MPI )
88+
ELSE( HDF5_FOUND )
89+
MESSAGE( FATAL_ERROR "HDF5 was requested but not found." )
90+
ENDIF( HDF5_FOUND )
91+
ENDIF( FIELDML_USE_HDF5 )
92+
7193
SET( CMAKE_PREFIX_PATH ${CMAKE_INSTALL_PREFIX} )
7294
FIND_PACKAGE( LibXml2 REQUIRED )
7395

@@ -112,11 +134,11 @@ ENDIF( "${CMAKE_BUILD_TYPE}" STREQUAL "Debug" )
112134
IF( WIN32 )
113135
ADD_DEFINITIONS( -D_CRT_SECURE_NO_WARNINGS )
114136
ENDIF( WIN32 )
115-
INCLUDE_DIRECTORIES( ${FIELDML_API_PUBLIC_HDRS} ${HDF5_INCLUDE_DIR} )
137+
INCLUDE_DIRECTORIES( ${FIELDML_API_PUBLIC_HDRS} ${HDF5_INCLUDE_DIRS} ${MPI_INCLUDE_DIRS} )
116138

117139
# Create library
118140
ADD_LIBRARY( ${LIBRARY_TARGET_NAME} ${LIBRARY_BUILD_TYPE} ${FIELDML_IO_API_SRCS} ${FIELDML_IO_API_PUBLIC_HDRS} ${FIELDML_IO_API_PRIVATE_HDRS} ${LIBRARY_WIN32_XTRAS} )
119-
TARGET_LINK_LIBRARIES( ${LIBRARY_TARGET_NAME} ${HDF5_LIBRARY} )
141+
TARGET_LINK_LIBRARIES( ${LIBRARY_TARGET_NAME} ${HDF5_MINE_LIBRARIES} ${MPI_MINE_LIBRARIES} )
120142

121143
# Install targets
122144
IF( WIN32 AND NOT ${UPPERCASE_LIBRARY_TARGET_NAME}_BUILD_STATIC_LIB )

io/src/ArrayDataReader.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@
4848

4949
using namespace std;
5050

51-
ArrayDataReader * ArrayDataReader::create( FieldmlIoContext *context, const string root, FmlObjectHandle source )
51+
ArrayDataReader * ArrayDataReader::createInternal( FieldmlIoContext *context, const string root,
52+
FmlObjectHandle source, void *buffer )
5253
{
5354
ArrayDataReader *reader = NULL;
5455

@@ -73,7 +74,7 @@ ArrayDataReader * ArrayDataReader::create( FieldmlIoContext *context, const stri
7374
}
7475
else if( format == StringUtil::PLAIN_TEXT_NAME )
7576
{
76-
reader = TextArrayDataReader::create( context, root, source );
77+
reader = TextArrayDataReader::create( context, root, source, buffer );
7778
}
7879
else
7980
{
@@ -83,6 +84,17 @@ ArrayDataReader * ArrayDataReader::create( FieldmlIoContext *context, const stri
8384
return reader;
8485
}
8586

87+
ArrayDataReader * ArrayDataReader::create( FieldmlIoContext *context, const string root, FmlObjectHandle source )
88+
{
89+
return ArrayDataReader::createInternal( context, root, source, 0 );
90+
}
91+
92+
93+
ArrayDataReader * ArrayDataReader::createWithBuffer( FieldmlIoContext *context, const string root, FmlObjectHandle source, void *buffer )
94+
{
95+
return ArrayDataReader::createInternal( context, root, source, buffer );
96+
}
97+
8698

8799
ArrayDataReader::ArrayDataReader( FieldmlIoContext *_context ) :
88100
context( _context )

io/src/ArrayDataReader.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,23 @@
4444

4545
#include "FieldmlIoContext.h"
4646

47+
enum ArrayDataSourceType
48+
{
49+
ARRAY_DATA_SOURCE_UNKNOWN,
50+
ARRAY_DATA_SOURCE_DEFAULT,
51+
ARRAY_DATA_SOURCE_BUFFER,
52+
};
53+
4754
class ArrayDataReader
4855
{
4956
protected:
5057
FieldmlIoContext * const context;
5158

5259
ArrayDataReader( FieldmlIoContext *_context );
5360

61+
static ArrayDataReader *createInternal( FieldmlIoContext *context, const std::string root,
62+
FmlObjectHandle source, void *buffer );
63+
5464
public:
5565
virtual FmlIoErrorNumber readIntSlab( const int *offsets, const int *sizes, int *valueBuffer ) = 0;
5666

@@ -63,7 +73,10 @@ class ArrayDataReader
6373

6474
virtual ~ArrayDataReader();
6575

66-
static ArrayDataReader *create( FieldmlIoContext *context, const std::string root, FmlObjectHandle source );
76+
static ArrayDataReader *create( FieldmlIoContext *context, const std::string root, FmlObjectHandle source);
77+
78+
static ArrayDataReader *createWithBuffer( FieldmlIoContext *context, const std::string root,
79+
FmlObjectHandle source, void *buffer );
6780
};
6881

6982

io/src/FieldmlIoApi.cpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ FmlIoErrorNumber FieldmlIo_GetLastError()
7272
return FieldmlIoSession::getSession().getLastError();
7373
}
7474

75-
76-
FmlReaderHandle Fieldml_OpenReader( FmlSessionHandle handle, FmlObjectHandle objectHandle )
75+
FmlReaderHandle Fieldml_OpenReaderInternal( FmlSessionHandle handle, FmlObjectHandle objectHandle,
76+
ArrayDataSourceType sourceType, void *buffer)
7777
{
7878
if( Fieldml_IsObjectLocal( handle, objectHandle, 0 ) != 1 )
7979
{
@@ -92,7 +92,11 @@ FmlReaderHandle Fieldml_OpenReader( FmlSessionHandle handle, FmlObjectHandle obj
9292
}
9393
else
9494
{
95-
reader = ArrayDataReader::create( FieldmlIoSession::getSession().createContext( handle ), root, objectHandle );
95+
if (sourceType == ARRAY_DATA_SOURCE_DEFAULT)
96+
reader = ArrayDataReader::create( FieldmlIoSession::getSession().createContext( handle ), root, objectHandle);
97+
else
98+
reader = ArrayDataReader::createWithBuffer( FieldmlIoSession::getSession().createContext( handle ),
99+
root, objectHandle, buffer);
96100
}
97101
Fieldml_FreeString(region_string);
98102
}
@@ -111,6 +115,16 @@ FmlReaderHandle Fieldml_OpenReader( FmlSessionHandle handle, FmlObjectHandle obj
111115
}
112116

113117

118+
FmlReaderHandle Fieldml_OpenReader( FmlSessionHandle handle, FmlObjectHandle objectHandle)
119+
{
120+
return Fieldml_OpenReaderInternal(handle, objectHandle, ARRAY_DATA_SOURCE_DEFAULT, 0);
121+
}
122+
123+
FmlReaderHandle Fieldml_OpenReaderWithBuffer( FmlSessionHandle handle, FmlObjectHandle objectHandle, void *buffer)
124+
{
125+
return Fieldml_OpenReaderInternal(handle, objectHandle, ARRAY_DATA_SOURCE_BUFFER, buffer);
126+
}
127+
114128
FmlIoErrorNumber Fieldml_ReadIntSlab( FmlReaderHandle readerHandle, const int *offsets, const int *sizes, int *valueBuffer )
115129
{
116130
ArrayDataReader *reader = FieldmlIoSession::getSession().handleToReader( readerHandle );

0 commit comments

Comments
 (0)