Skip to content
Snippets Groups Projects
Commit fd2926f7 authored by Atkins, Charles Vernon's avatar Atkins, Charles Vernon Committed by GitHub
Browse files

Merge pull request #159 from chuckatkins/re-implement-mpidummy-with-stdio

mpidummy: Re-implement using std C and C++ for portability
parents 8f30141f ebcf7eb8
No related branches found
No related tags found
No related merge requests found
...@@ -15,20 +15,24 @@ ...@@ -15,20 +15,24 @@
#include "mpidummy.h" #include "mpidummy.h"
#include <sys/time.h> /*
#include <sys/types.h>
#include <unistd.h>
#define __STDC_FORMAT_MACROS #define __STDC_FORMAT_MACROS
#include <cinttypes> #include <cinttypes>
#include <cstdint> #include <cstdint>
#include <cstdio>
#include <cstring> #include <cstring>
#if defined(__APPLE__) || defined(__WIN32__) || defined(__CYGWIN__) #if defined(__APPLE__) || defined(__WIN32__) || defined(__CYGWIN__)
#define lseek64 lseek #define lseek64 lseek
#define open64 open #define open64 open
#endif #endif
*/
#include <cinttypes>
#include <cstdio>
#include <cstring>
#include <chrono>
#include <string>
namespace adios2 namespace adios2
{ {
...@@ -135,11 +139,11 @@ int MPI_Gather(const void *sendbuf, int sendcnt, MPI_Datatype sendtype, ...@@ -135,11 +139,11 @@ int MPI_Gather(const void *sendbuf, int sendcnt, MPI_Datatype sendtype,
if (ier == MPI_SUCCESS) if (ier == MPI_SUCCESS)
{ {
memcpy(recvbuf, sendbuf, nsent); std::memcpy(recvbuf, sendbuf, nsent);
} }
else else
{ {
snprintf(mpierrmsg, ier, "could not gather data\n"); std::snprintf(mpierrmsg, ier, "could not gather data\n");
} }
return ier; return ier;
...@@ -215,11 +219,11 @@ int MPI_Scatter(const void *sendbuf, int sendcnt, MPI_Datatype sendtype, ...@@ -215,11 +219,11 @@ int MPI_Scatter(const void *sendbuf, int sendcnt, MPI_Datatype sendtype,
if (ier == MPI_SUCCESS) if (ier == MPI_SUCCESS)
{ {
memcpy(recvbuf, sendbuf, nsent); std::memcpy(recvbuf, sendbuf, nsent);
} }
else else
{ {
snprintf(mpierrmsg, ier, "could not scatter data\n"); std::snprintf(mpierrmsg, ier, "could not scatter data\n");
} }
return ier; return ier;
...@@ -277,24 +281,39 @@ int MPI_Wait(MPI_Request * /*request*/, MPI_Status * /*status*/) { return 0; } ...@@ -277,24 +281,39 @@ int MPI_Wait(MPI_Request * /*request*/, MPI_Status * /*status*/) { return 0; }
int MPI_File_open(MPI_Comm /*comm*/, const char *filename, int amode, int MPI_File_open(MPI_Comm /*comm*/, const char *filename, int amode,
MPI_Info /*info*/, MPI_File *fh) MPI_Info /*info*/, MPI_File *fh)
{ {
*fh = open64(filename, amode); std::string mode;
if (*fh == -1) if (amode | MPI_MODE_RDONLY)
{ {
snprintf(mpierrmsg, MPI_MAX_ERROR_STRING, "File not found: %s", mode += "r";
filename); }
if (amode | MPI_MODE_WRONLY)
{
mode += "w";
}
if (amode | MPI_MODE_APPEND)
{
mode += "a";
}
mode += "b";
*fh = fopen(filename, mode.c_str());
if (!*fh)
{
std::snprintf(mpierrmsg, MPI_MAX_ERROR_STRING, "File not found: %s",
filename);
return -1; return -1;
} }
return MPI_SUCCESS; return MPI_SUCCESS;
} }
int MPI_File_close(MPI_File *fh) { return close(*fh); } int MPI_File_close(MPI_File *fh) { return fclose(*fh); }
int MPI_File_get_size(MPI_File fh, MPI_Offset *size) int MPI_File_get_size(MPI_File fh, MPI_Offset *size)
{ {
uint64_t curpos = lseek64(fh, 0, SEEK_CUR); // get the current seek pos long curpos = ftell(fh);
uint64_t endpos = fseek(fh, 0, SEEK_END); // go to end, returned is the size in bytes
lseek64(fh, 0, SEEK_END); // go to end, returned is the size in bytes long endpos = ftell(fh);
lseek64(fh, curpos, SEEK_SET); // go back where we were fseek(fh, curpos, SEEK_SET); // go back where we were
*size = static_cast<MPI_Offset>(endpos); *size = static_cast<MPI_Offset>(endpos);
// printf("MPI_File_get_size: fh=%d, size=%lld\n", fh, *size); // printf("MPI_File_get_size: fh=%d, size=%lld\n", fh, *size);
return MPI_SUCCESS; return MPI_SUCCESS;
...@@ -304,14 +323,15 @@ int MPI_File_read(MPI_File fh, void *buf, int count, MPI_Datatype datatype, ...@@ -304,14 +323,15 @@ int MPI_File_read(MPI_File fh, void *buf, int count, MPI_Datatype datatype,
MPI_Status *status) MPI_Status *status)
{ {
// FIXME: int count can read only 2GB (*datatype size) array at max // FIXME: int count can read only 2GB (*datatype size) array at max
uint64_t bytes_to_read = static_cast<uint64_t>(count) * datatype; size_t bytes_to_read = static_cast<size_t>(count) * datatype;
uint64_t bytes_read; size_t bytes_read;
bytes_read = read(fh, buf, bytes_to_read); bytes_read = fread(buf, 1, bytes_to_read, fh);
if (bytes_read != bytes_to_read) if (bytes_read != bytes_to_read)
{ {
snprintf(mpierrmsg, MPI_MAX_ERROR_STRING, std::snprintf(mpierrmsg, MPI_MAX_ERROR_STRING,
"could not read %" PRId64 " bytes. read only: %" PRId64 "\n", "could not read %" PRId64 " bytes. read only: %" PRId64
bytes_to_read, bytes_read); "\n",
bytes_to_read, bytes_read);
return -2; return -2;
} }
*status = bytes_read; *status = bytes_read;
...@@ -322,10 +342,7 @@ int MPI_File_read(MPI_File fh, void *buf, int count, MPI_Datatype datatype, ...@@ -322,10 +342,7 @@ int MPI_File_read(MPI_File fh, void *buf, int count, MPI_Datatype datatype,
int MPI_File_seek(MPI_File fh, MPI_Offset offset, int whence) int MPI_File_seek(MPI_File fh, MPI_Offset offset, int whence)
{ {
lseek64(fh, offset, whence); return fseek(fh, offset, whence) == MPI_SUCCESS;
// printf("MPI_File_seek: fh=%d, offset=%lld, whence=%d\n", fh, off,
// whence);
return MPI_SUCCESS;
} }
int MPI_Get_count(const MPI_Status *status, MPI_Datatype, int *count) int MPI_Get_count(const MPI_Status *status, MPI_Datatype, int *count)
...@@ -336,24 +353,23 @@ int MPI_Get_count(const MPI_Status *status, MPI_Datatype, int *count) ...@@ -336,24 +353,23 @@ int MPI_Get_count(const MPI_Status *status, MPI_Datatype, int *count)
int MPI_Error_string(int /*errorcode*/, char *string, int *resultlen) int MPI_Error_string(int /*errorcode*/, char *string, int *resultlen)
{ {
// sprintf(string, "Dummy lib does not know error strings. // std::sprintf(string, "Dummy lib does not know error strings.
// Code=%d\n",errorcode); // Code=%d\n",errorcode);
strcpy(string, mpierrmsg); std::strcpy(string, mpierrmsg);
*resultlen = strlen(string); *resultlen = std::strlen(string);
return MPI_SUCCESS; return MPI_SUCCESS;
} }
double MPI_Wtime() double MPI_Wtime()
{ {
// Implementation not tested std::chrono::duration<double> now =
timeval tv = {0, 0}; std::chrono::high_resolution_clock::now().time_since_epoch();
gettimeofday(&tv, nullptr); return now.count();
return tv.tv_sec + tv.tv_usec * 1e-6;
} }
int MPI_Get_processor_name(char *name, int *resultlen) int MPI_Get_processor_name(char *name, int *resultlen)
{ {
sprintf(name, "0"); std::sprintf(name, "0");
*resultlen = 1; *resultlen = 1;
return 0; return 0;
} }
......
...@@ -11,26 +11,22 @@ ...@@ -11,26 +11,22 @@
of the API of the API
*/ */
#include <cstdint>
#include <cstdio>
/// \cond EXCLUDE_FROM_DOXYGEN /// \cond EXCLUDE_FROM_DOXYGEN
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
/// \endcond /// \endcond
#include "adios2/ADIOSConfig.h"
namespace adios2 namespace adios2
{ {
typedef int MPI_Comm; typedef int MPI_Comm;
typedef uint64_t MPI_Status; typedef std::uint64_t MPI_Status;
typedef uint64_t MPI_Request; typedef std::uint64_t MPI_Request;
typedef int MPI_File; typedef std::FILE *MPI_File;
typedef int MPI_Info; typedef int MPI_Info;
typedef int MPI_Datatype; /* Store the byte size of a type in such vars */ typedef int MPI_Datatype; /* Store the byte size of a type in such vars */
typedef uint64_t MPI_Offset; typedef long MPI_Offset;
typedef int MPI_Fint; typedef int MPI_Fint;
#define MPI_SUCCESS 0 #define MPI_SUCCESS 0
...@@ -40,7 +36,15 @@ typedef int MPI_Fint; ...@@ -40,7 +36,15 @@ typedef int MPI_Fint;
#define MPI_ERR_TAG 4 /* Invalid tag argument */ #define MPI_ERR_TAG 4 /* Invalid tag argument */
#define MPI_ERR_COMM 5 /* Invalid communicator */ #define MPI_ERR_COMM 5 /* Invalid communicator */
#define MPI_MAX_ERROR_STRING 512 #define MPI_MAX_ERROR_STRING 512
#define MPI_MODE_RDONLY O_RDONLY #define MPI_MODE_RDONLY 1
#define MPI_MODE_WRONLY 2
#define MPI_MODE_RDWR (MPI_MODE_RDONLY | MPI_MODE_RDONLY)
#define MPI_MODE_CREATE MPI_MODE_WRONLY
#define MPI_MODE_EXCL 0
#define MPI_MODE_DELETE_ON_CLOSE 0
#define MPI_MODE_UNIQUE_OPEN 0
#define MPI_MODE_SEQUENTIAL 0
#define MPI_MODE_APPEND 4
#define MPI_SEEK_SET SEEK_SET #define MPI_SEEK_SET SEEK_SET
#define MPI_SEEK_CUR SEEK_CUR #define MPI_SEEK_CUR SEEK_CUR
#define MPI_SEEK_END SEEK_END #define MPI_SEEK_END SEEK_END
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment