Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use C++17 for file system operations in order to support Windows
Browse files Browse the repository at this point in the history
WeiqunZhang committed May 29, 2020
1 parent 1b30da6 commit fa51fee
Showing 18 changed files with 185 additions and 90 deletions.
4 changes: 0 additions & 4 deletions Src/Amr/AMReX_Amr.cpp
Original file line number Diff line number Diff line change
@@ -12,10 +12,6 @@
#include <omp.h>
#endif

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#include <AMReX_Geometry.H>
#include <AMReX_TagBox.H>
#include <AMReX_Array.H>
1 change: 0 additions & 1 deletion Src/Amr/AMReX_AmrLevel.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

#include <sstream>

#include <unistd.h>
#include <memory>
#include <limits>

1 change: 0 additions & 1 deletion Src/Amr/AMReX_AsyncFillPatch.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include <sstream>
#include <unistd.h>
#include <memory>
#include <limits>

2 changes: 0 additions & 2 deletions Src/Amr/AMReX_StateData.cpp
Original file line number Diff line number Diff line change
@@ -2,8 +2,6 @@
#include <iostream>
#include <algorithm>

#include <unistd.h>

#include <AMReX_RealBox.H>
#include <AMReX_StateData.H>
#include <AMReX_StateDescriptor.H>
10 changes: 2 additions & 8 deletions Src/Base/AMReX.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#include <unistd.h>
#include <cstdio>
#include <cstdlib>
#include <cstring>
@@ -10,6 +9,7 @@
#include <vector>
#include <algorithm>

#include <AMReX_FileSystem.H>
#include <AMReX_ParallelDescriptor.H>
#include <AMReX.H>
#include <AMReX_BaseFab.H>
@@ -315,13 +315,7 @@ amrex::Initialize (int& argc, char**& argv, bool build_parm_parse,
if (argc > 0)
{
if (argv[0][0] != '/') {
constexpr int bufSize = 1024;
char temp[bufSize];
char *rCheck = getcwd(temp, bufSize);
if(rCheck == 0) {
amrex::Abort("**** Error: getcwd buffer too small.");
}
system::exename = temp;
system::exename = FileSystem::CurrentPath();
system::exename += "/";
}
system::exename += argv[0];
9 changes: 6 additions & 3 deletions Src/Base/AMReX_BLBackTrace.cpp
Original file line number Diff line number Diff line change
@@ -4,14 +4,13 @@
#include <cstring>
#include <cstdio>

#include <unistd.h>

#include <AMReX_BLBackTrace.H>
#include <AMReX_ParallelDescriptor.H>
#include <AMReX_Print.H>
#include <AMReX_VisMF.H>
#include <AMReX_AsyncOut.H>
#include <AMReX.H>
#include <AMReX_Utility.H>

#ifdef AMREX_TINY_PROFILING
#include <AMReX_TinyProfiler.H>
@@ -25,6 +24,10 @@
#define AMREX_BACKTRACE_SUPPORTED 1
#endif

#ifdef __linux__
#include <unistd.h>
#endif

namespace amrex {

#ifdef AMREX_BACKTRACING
@@ -107,7 +110,7 @@ BLBackTrace::handler(int s)
#endif

if (ParallelDescriptor::NProcs() > 1) {
sleep(3);
amrex::Sleep(3);
}

#endif
20 changes: 20 additions & 0 deletions Src/Base/AMReX_FileSystem.H
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef AMREX_FILE_SYSTEM_H_
#define AMREX_FILE_SYSTEM_H_

#include <string>

namespace amrex {
namespace FileSystem {

std::string
CurrentPath ();

bool
Exists (std::string const& filename);

bool
Remove (std::string const& filename);

}}

#endif
77 changes: 77 additions & 0 deletions Src/Base/AMReX_FileSystem.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#include <AMReX_FileSystem.H>
#include <AMReX_Print.H>
#include <AMReX.H>

#if defined(_WIN32) || __cplusplus >= 201703L

#include <filesystem>
#include <system_error>

namespace amrex {
namespace FileSystem {

bool
Exists (std::string const& filename)
{
std::error_code ec;
return std::filesystem::exists(std::filesystem::path{filename});
}

std::string
CurrentPath ()
{
std::error_code ec;
auto path = std::filesystem::current_path(ec);
if (ec) {
amrex::AllPrint() << "amrex::FileSystem::CurrentPath failed. " << ec.message() << std::endl;
}
return path.string();
}

bool
Remove (std::string const& filename)
{
std::error_code ec;
bool r = std::filesystem::remove(std::filesystem::path(filename),ec);
return (!ec and r);
}

}}

#else

#include <cstddef>
#include <unistd.h>
#include <sys/stat.h>

namespace amrex {
namespace FileSystem {

bool
Exists (std::string const& filename)
{
struct stat statbuff;
return(lstat(filename.c_str(), &statbuff) != -1);
}

std::string
CurrentPath ()
{
constexpr int bufSize = 1024;
char temp[bufSize];
char *rCheck = getcwd(temp, bufSize);
if(rCheck == 0) {
amrex::Abort("**** Error: getcwd buffer too small.");
}
return std::string(rCheck);
}

bool
Remove (std::string const& filename)
{
return unlink(filename.c_str());
}

}}

#endif
1 change: 0 additions & 1 deletion Src/Base/AMReX_ParallelDescriptor.cpp
Original file line number Diff line number Diff line change
@@ -4,7 +4,6 @@
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <unistd.h>
#include <sstream>
#include <stack>
#include <list>
15 changes: 8 additions & 7 deletions Src/Base/AMReX_Utility.H
Original file line number Diff line number Diff line change
@@ -12,10 +12,6 @@
#include <limits>
#include <cfloat>

#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>

#include <AMReX_BLassert.H>
#include <AMReX_REAL.H>
#include <AMReX_INT.H>
@@ -27,6 +23,13 @@
#include <AMReX_ParallelDescriptor.H>
#include <AMReX_Random.H>
#include <AMReX_GpuQualifiers.H>
#include <AMReX_FileSystem.H>

#ifdef __WIN32
typedef unsigned short mode_t
#else
#include <sys/types.h> // for mode_t
#endif

namespace amrex
{
@@ -91,8 +94,6 @@ namespace amrex
void CreateDirectoryFailed (const std::string& dir);
//! Output a message and abort when couldn't open the file.
void FileOpenFailed (const std::string& dir);
//! Attempt to unlink the file. Ignore any errors.
void UnlinkFile (const std::string& file);
/**
* \brief Check if a file already exists.
* Return true if the filename is an existing file, directory,
@@ -249,7 +250,7 @@ namespace amrex

template<class T> void BroadcastArray(Vector<T> &aT, int myLocalId, int rootId, const MPI_Comm &localComm);

void USleep(double sleepsec);
void Sleep (double sleepsec); // Sleep for sleepsec seconds


using MaxResSteadyClock = std::conditional<std::chrono::high_resolution_clock::is_steady,
44 changes: 24 additions & 20 deletions Src/Base/AMReX_Utility.cpp
Original file line number Diff line number Diff line change
@@ -4,24 +4,21 @@
#include <cmath>
#include <cstdio>
#include <ctime>
#include <chrono>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <set>
#include <random>

#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#include <thread>

#include <AMReX_BLFort.H>
#include <AMReX_REAL.H>
#include <AMReX.H>
#include <AMReX_Utility.H>
#include <AMReX_BLassert.H>
#include <AMReX_BLProfiler.H>
#include <AMReX_Print.H>

#include <AMReX_FileSystem.H>
#include <AMReX_ParallelDescriptor.H>
#include <AMReX_BoxArray.H>
#include <AMReX_Print.H>
@@ -30,12 +27,17 @@
#include <omp.h>
#endif

#include <sys/types.h>
#if defined(_WIN32) || __cplusplus >= 201703L
#include <filesystem>
#include <system_error>
#else
#include <sys/stat.h> // for mkdir
#endif

#include <sys/times.h>
#include <sys/time.h>
#include <sys/param.h>
#include <unistd.h>

#include <errno.h>

using std::ostringstream;

@@ -166,6 +168,15 @@ bool
amrex::UtilCreateDirectory (const std::string& path,
mode_t mode, bool verbose)
{
#if defined(_WIN32) || __cplusplus >= 201703L
std::error_code ec;
bool ret = std::filesystem::create_directories(std::filesystem::path{path}, ec);
if (ec and verbose) {
amrex::AllPrint() << "amrex::UtilCreateDirectory failed to create "
<< path << ": " << ec.message() << std::endl;
}
return !ec;
#else
bool retVal(false);
Vector<std::pair<std::string, int> > pathError;

@@ -251,6 +262,7 @@ amrex::UtilCreateDirectory (const std::string& path,
}

return retVal;
#endif
}

void
@@ -269,17 +281,10 @@ amrex::FileOpenFailed (const std::string& file)
amrex::Error(msg.c_str());
}

void
amrex::UnlinkFile (const std::string& file)
{
unlink(file.c_str());
}

bool
amrex::FileExists(const std::string &filename)
{
struct stat statbuff;
return(::lstat(filename.c_str(), &statbuff) != -1);
return amrex::FileSystem::Exists(filename);
}

std::string
@@ -1041,9 +1046,8 @@ void amrex::BroadcastStringArray(Vector<std::string> &bSA, int myLocalId, int ro
}
}

void amrex::USleep(double sleepsec) {
constexpr unsigned int msps = 1000000;
usleep(static_cast<useconds_t>(sleepsec * msps));
void amrex::Sleep(double sleepsec) {
std::this_thread::sleep_for(std::chrono::duration<double>(sleepsec));
}


2 changes: 2 additions & 0 deletions Src/Base/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -54,6 +54,8 @@ target_sources( amrex
AMReX_Functional.H
AMReX_Utility.H
AMReX_Utility.cpp
AMReX_FileSystem.H
AMReX_FileSystem.cpp
AMReX_Reduce.H
AMReX_Scan.H
AMReX_Partition.H
3 changes: 3 additions & 0 deletions Src/Base/Make.package
Original file line number Diff line number Diff line change
@@ -24,6 +24,9 @@ C$(AMREX_BASE)_sources += AMReX_ParmParse.cpp AMReX_parmparse_fi.cpp AMReX_Utili
C$(AMREX_BASE)_headers += AMReX_ParmParse.H AMReX_Utility.H AMReX_BLassert.H AMReX_ArrayLim.H
C$(AMREX_BASE)_headers += AMReX_Functional.H AMReX_Reduce.H AMReX_Scan.H AMReX_Partition.H

C$(AMREX_BASE)_headers += AMReX_FileSystem.H
C$(AMREX_BASE)_sources += AMReX_FileSystem.cpp

C$(AMREX_BASE)_headers += AMReX_Random.H
C$(AMREX_BASE)_sources += AMReX_Random.cpp

4 changes: 2 additions & 2 deletions Src/Particle/AMReX_ParticleIO.H
Original file line number Diff line number Diff line change
@@ -671,7 +671,7 @@ ParticleContainer<NStructReal, NStructInt, NArrayReal, NArrayInt>
if (cnt[i] == 0)
{
std::string FullFileName = NFilesIter::FileName(i, filePrefix);
amrex::UnlinkFile(FullFileName.c_str());
FileSystem::Remove(FullFileName);
}
}
}
@@ -793,7 +793,7 @@ ParticleContainer<NStructReal, NStructInt, NArrayReal, NArrayInt>
for(int i(0), N = cnt.size(); i < N; ++i) {
if(cnt[i] == 0) {
std::string FullFileName = NFilesIter::FileName(i, filePrefixPrePost[lev]);
amrex::UnlinkFile(FullFileName.c_str());
FileSystem::Remove(FullFileName);
}
}
}
2 changes: 1 addition & 1 deletion Tests/AsyncOut/multifab/main.cpp
Original file line number Diff line number Diff line change
@@ -113,7 +113,7 @@ void main_main ()
if (ip == ParallelDescriptor::MyProc()) {
amrex::AllPrint() << "Proc. " << ip << " number of boxes = " << mfs[0].local_size() << std::endl;
}
amrex::USleep(0.001);
amrex::Sleep(0.001);
ParallelDescriptor::Barrier();
}

Loading

0 comments on commit fa51fee

Please sign in to comment.