Commit 0c88a665 authored by kraus's avatar kraus
Browse files

replace &(someVector[0]) with someVector.data() where the vector written but...

replace &(someVector[0]) with someVector.data() where the vector written but only read and where we can't be sure that if it has elements
parent e21d9ac4
......@@ -99,6 +99,9 @@ elseif (${CMAKE_CXX_COMPILER_ID} MATCHES "GNU")
add_link_options (-fopenmp)
endif ()
# Enables extra error checking in the form of precondition assertion, such
# as bounds checking and null pointer checks when dereferencing smart pointers
add_compile_options(-Wp,-D_GLIBCXX_ASSERTIONS)
else ()
message (STATUS "Unknown C++ compiler. Please use the GNU or Intel compiler, if you are having problems.")
endif ()
......@@ -271,4 +274,4 @@ install (
FILES ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config_install.cmake
DESTINATION "${CMAKE_INSTALL_PREFIX}/lib/cmake/${PROJECT_NAME}"
RENAME ${PROJECT_NAME}Config.cmake
)
)
\ No newline at end of file
set (_SRCS
CRC.cpp
Communicate.cpp
CommCreator.cpp
CommMPI.cpp
Communicate.cpp
Format.cpp
MessageFunctions.cpp
Formatter.cpp
MsgBuffer.cpp
)
set (_HDRS
CRC.h
CommCreator.h
CommMPI.h
Communicate.h
CRC.h
DataTypes.h
Formatter.h
GlobalComm.hpp
Format.h
GlobalComm.h
Message.hpp
GlobalComm.hpp
Message.h
Message.hpp
MsgBuffer.h
Operations.h
TagMaker.h
Tags.h
......@@ -30,4 +32,4 @@ include_directories (
add_ippl_sources (${_SRCS})
add_ippl_headers (${_HDRS})
install (FILES ${_HDRS} DESTINATION include/Message)
install (FILES ${_HDRS} DESTINATION include/Message)
\ No newline at end of file
//
// Class Format
// Format class to allow serializing message objects into plain buffers
// to send directly with mpi calls or similar means
//
// Copyright (c) 2008 - 2020, Paul Scherrer Institut, Villigen PSI, Switzerland
// All rights reserved
//
// This file is part of OPAL.
//
// OPAL is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// You should have received a copy of the GNU General Public License
// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
//
#include "Message/Format.h"
Format::Format(Message *msg)
{
items = msg->size();
size = 0;
format_array.resize(2*items);
for (unsigned int i=0; i<items; ++i)
{
Message::MsgItem &msgitem = msg->item(i);
format_array[2*i+0] = msgitem.numElems();
format_array[2*i+1] = msgitem.numBytes();
size += format_array[2*i+1];
}
}
void Format::print()
{
std::cout << "size: " << size << std::endl;
for (unsigned int i=0; i<items; ++i)
{
std::cout << "entry " << i << ": " << format_array[2*i+0]
<< " elements " << format_array[2*i+1] << " bytes\n";
}
}
\ No newline at end of file
//
// Class Format
// Format class to allow serializing message objects into plain buffers
// to send directly with mpi calls or similar means
//
// Copyright (c) 2008 - 2020, Paul Scherrer Institut, Villigen PSI, Switzerland
// All rights reserved
//
// This file is part of OPAL.
//
// OPAL is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// You should have received a copy of the GNU General Public License
// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
//
#ifndef FORMATTER_H
#define FORMATTER_H
#include <algorithm>
#include <cstring>
#include <vector>
#include "Message/Message.h"
class Format {
public:
Format(Message*);
unsigned int getItemCount() {
return items;
}
unsigned int getSize() {
return size;
}
unsigned int getFormatSize() {
return 2 * items * sizeof(int);
}
unsigned int getItemElems(int i) {
return format_array[2 * i + 0];
}
unsigned int getItemBytes(int i) {
return format_array[2 * i + 1];
}
void print();
private:
unsigned int items, size;
std::vector<unsigned int> format_array;
};
#endif
\ No newline at end of file
// -*- C++ -*-
/***************************************************************************
*
* The IPPL Framework
*
*
* Visit http://people.web.psi.ch/adelmann/ for more details
*
***************************************************************************/
#ifndef FORMATTER_H
#define FORMATTER_H
#include "Message/Message.h"
#include <cstring>
#include <vector>
#include <algorithm>
/*
* Format and MsgBuffer class to allow serializing message objects into plain buffers
* to send directly with mpi calls or similar means
*/
class Format
{
public:
Format(Message*);
unsigned int getItemCount()
{
return items;
}
unsigned int getSize()
{
return size;
}
unsigned int getFormatSize()
{
return 2*items*sizeof(int);
}
unsigned int getItemElems(int i)
{
return format_array[2*i+0];
}
unsigned int getItemBytes(int i)
{
return format_array[2*i+1];
}
void print();
private:
unsigned int items, size;
std::vector<unsigned int> format_array;
};
class MsgBuffer
{
public:
//creates buffer with space to hold count messages of format f
MsgBuffer(Format *f, int count, int offset = 0);
MsgBuffer(Format *f, char* d, int size);
bool add(Message*);
Message* get();
template<class T>
void get(T &v)
{
std::memcpy(&v, data.data()+readpos, sizeof(T));
readpos += sizeof(T);
}
template<class T>
void put(T &v)
{
std::memcpy(data.data()+writepos, &v, sizeof(T));
writepos += sizeof(T);
}
int getSize()
{
return writepos;
}
void* getBuffer()
{
return data.data();
}
Format* getFormat() { return format; }
~MsgBuffer();
private:
Format *format;
unsigned int datasize, writepos, readpos;
std::vector<char> data;
};
#endif // FORMATTER_H
#include "Message/Formatter.h"
Format::Format(Message *msg)
{
items = msg->size();
size = 0;
format_array.resize(2*items);
for (unsigned int i=0; i<items; ++i)
{
Message::MsgItem &msgitem = msg->item(i);
format_array[2*i+0] = msgitem.numElems();
format_array[2*i+1] = msgitem.numBytes();
size += format_array[2*i+1];
}
}
void Format::print()
{
std::cout << "size: " << size << std::endl;
for (unsigned int i=0; i<items; ++i)
{
std::cout << "entry " << i << ": " << format_array[2*i+0]
<< " elements " << format_array[2*i+1] << " bytes\n";
}
}
//
// Class MsgBuffer
// MsgBuffer class to allow serializing message objects into plain buffers
// to send directly with mpi calls or similar means
//
// Copyright (c) 2008 - 2020, Paul Scherrer Institut, Villigen PSI, Switzerland
// All rights reserved
//
// This file is part of OPAL.
//
// OPAL is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// You should have received a copy of the GNU General Public License
// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
//
#include "Message/MsgBuffer.h"
#include "Message/Format.h"
MsgBuffer::MsgBuffer(Format *f, int count, int offset)
: format(f), writepos(0), readpos(0)
......@@ -94,4 +89,4 @@ Message* MsgBuffer::get()
}
return msg;
}
}
\ No newline at end of file
//
// Class MsgBuffer
// MsgBuffer class to allow serializing message objects into plain buffers
// to send directly with mpi calls or similar means
//
// Copyright (c) 2008 - 2020, Paul Scherrer Institut, Villigen PSI, Switzerland
// All rights reserved
//
// This file is part of OPAL.
//
// OPAL is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// You should have received a copy of the GNU General Public License
// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
//
#ifndef MSGBUFFER_H
#define MSGBUFFER_H
#include <algorithm>
#include <cstring>
#include <vector>
#include "Message/Message.h"
class Format;
class MsgBuffer {
public:
// creates buffer with space to hold count messages of format f
MsgBuffer(Format* f, int count, int offset = 0);
MsgBuffer(Format* f, char* d, int size);
bool add(Message*);
Message* get();
template <class T>
void get(T& v) {
std::memcpy(&v, data.data() + readpos, sizeof(T));
readpos += sizeof(T);
}
template <class T>
void put(T& v) {
std::memcpy(data.data() + writepos, &v, sizeof(T));
writepos += sizeof(T);
}
int getSize() {
return writepos;
}
void* getBuffer() {
char* data_ptr = data.empty() ? static_cast<char*>(0) : &(data[0]);
return data_ptr;
}
Format* getFormat() {
return format;
}
~MsgBuffer();
private:
Format* format;
unsigned int datasize, writepos, readpos;
std::vector<char> data;
};
#endif
\ No newline at end of file
......@@ -14,7 +14,8 @@
#include "Message/Message.h"
#include "Message/Communicate.h"
#include "Message/Formatter.h"
#include "Message/Format.h"
#include "Message/MsgBuffer.h"
template <class T, unsigned Dim, class Mesh, class CachingPolicy> class ParticleSpatialLayout;
......@@ -233,7 +234,8 @@ template<class C>
}
//wait for communication to finish and clean up buffers
MPI_Waitall(requests.size(), &(requests[0]), MPI_STATUSES_IGNORE);
MPI_Request* requests_ptr = requests.empty()? static_cast<MPI_Request*>(0): &(requests[0]);
MPI_Waitall(requests.size(), requests_ptr, MPI_STATUSES_IGNORE);
for (unsigned int j = 0; j<buffers.size(); ++j)
{
delete buffers[j]->getFormat();
......@@ -259,4 +261,4 @@ private:
std::map<unsigned, std::list<std::pair<NDRegion<T,Dim>, Offset_t> > > regions;
};
#endif
\ No newline at end of file
#endif
......@@ -100,7 +100,8 @@
#include "AppTypes/Vektor.h"
#include "DataSource/DataSource.h"
#include "DataSource/MakeDataSource.h"
#include "Message/Formatter.h"
#include "Message/Format.h"
#include "Message/MsgBuffer.h"
#include <vector>
#include <utility>
#include <iostream>
......@@ -400,4 +401,4 @@ private:
* $RCSfile: IpplParticleBase.h,v $ $Author: adelmann $
* $Revision: 1.1.1.1 $ $Date: 2003/01/23 07:40:28 $
* IPPL_VERSION_ID: $Id: IpplParticleBase.h,v 1.1.1.1 2003/01/23 07:40:28 adelmann Exp $
***************************************************************************/
\ No newline at end of file
***************************************************************************/
......@@ -26,6 +26,8 @@
#include "Particle/IpplParticleBase.h"
#include "Region/RegionLayout.h"
#include "Message/Message.h"
#include "Message/Format.h"
#include "Message/MsgBuffer.h"
#include "FieldLayout/FieldLayoutUser.h"
#include "Utility/IpplException.h"
......@@ -39,7 +41,6 @@
#include "BoxParticleCachingPolicy.h"
#include "Message/Formatter.h"
#include <mpi.h>
// forward declarations
......@@ -1197,7 +1198,8 @@ protected:
}
//wait for communication to finish and clean up buffers
MPI_Waitall(requests.size(), &(requests[0]), MPI_STATUSES_IGNORE);
MPI_Request* requests_ptr = requests.empty()? static_cast<MPI_Request*>(0): &(requests[0]);
MPI_Waitall(requests.size(), requests_ptr, MPI_STATUSES_IGNORE);
return LocalNum;
}
......@@ -1326,7 +1328,8 @@ protected:
}
//wait for communication to finish and clean up buffers
MPI_Waitall(requests.size(), &(requests[0]), 0);
MPI_Request* requests_ptr = requests.empty()? static_cast<MPI_Request*>(0): &(requests[0]);
MPI_Waitall(requests.size(), requests_ptr, 0);
return LocalNum;
}
......
......@@ -38,7 +38,8 @@
#include "BoxLibLayout.h"
#include "Message/Formatter.h"
#include "Message/Format.h"
#include "Message/MsgBuffer.h"
#include "Utility/PAssert.h"
#include "Utilities/OpalException.h"
......@@ -348,7 +349,8 @@ void BoxLibLayout<T, Dim>::update(AmrParticleBase< BoxLibLayout<T,Dim> >& PData,
}
//wait for communication to finish and clean up buffers
MPI_Waitall(requests.size(), &(requests[0]), MPI_STATUSES_IGNORE);
MPI_Request* requests_ptr = requests.empty()? static_cast<MPI_Request*>(0): &(requests[0]);
MPI_Waitall(requests.size(), requests_ptr, MPI_STATUSES_IGNORE);
for (unsigned int j = 0; j<buffers.size(); ++j)
{
delete buffers[j];
......@@ -479,7 +481,7 @@ void BoxLibLayout<T, Dim>::clearLevelMask(int lev) {
template <class T, unsigned Dim>
const std::unique_ptr<typename BoxLibLayout<T, Dim>::mask_t>&
const std::unique_ptr<typename BoxLibLayout<T, Dim>::mask_t>&
BoxLibLayout<T, Dim>::getLevelMask(int lev) const {
if ( lev >= (int)masks_m.size() ) {
throw OpalException("BoxLibLayout::getLevelMask()",
......@@ -856,4 +858,4 @@ void BoxLibLayout<T, Dim>::initBaseBox_m(int nGridPoints,
this->m_nlevels = ba.size();
}
#endif
\ No newline at end of file
#endif
......@@ -161,4 +161,4 @@ set (HDRS
changes.h
)
install (FILES ${HDRS} DESTINATION "${CMAKE_INSTALL_PREFIX}/include/src")
install (FILES ${HDRS} DESTINATION "${CMAKE_INSTALL_PREFIX}/include/src")
\ No newline at end of file
......@@ -399,22 +399,22 @@ void LossDataSink::saveH5(unsigned int setIdx) {
WRITE_STEPATTRIB_INT64("GlobalTrackStep", &(globalTrackStep_m[setIdx]), 1);
}
// Write all data
WRITE_DATA_FLOAT64 ("x", &x_m[startIdx]);
WRITE_DATA_FLOAT64 ("y", &y_m[startIdx]);
WRITE_DATA_FLOAT64 ("z", &z_m[startIdx]);
WRITE_DATA_FLOAT64 ("px", &px_m[startIdx]);
WRITE_DATA_FLOAT64 ("py", &py_m[startIdx]);
WRITE_DATA_FLOAT64 ("pz", &pz_m[startIdx]);
WRITE_DATA_FLOAT64 ("x", x_m.data() + startIdx);
WRITE_DATA_FLOAT64 ("y", y_m.data() + startIdx);
WRITE_DATA_FLOAT64 ("z", z_m.data() + startIdx);
WRITE_DATA_FLOAT64 ("px", px_m.data() + startIdx);
WRITE_DATA_FLOAT64 ("py", py_m.data() + startIdx);
WRITE_DATA_FLOAT64 ("pz", pz_m.data() + startIdx);
/// Write particle id numbers.
std::vector<h5_int64_t> larray(id_m.begin() + startIdx, id_m.end() );
WRITE_DATA_INT64 ("id", &larray[0]);
WRITE_DATA_INT64 ("id", larray.data());
if (hasTimeAttribute()) {
WRITE_DATA_FLOAT64 ("time", &time_m[startIdx]);
WRITE_DATA_FLOAT64 ("time", time_m.data() + startIdx);
larray.assign (turn_m.begin() + startIdx, turn_m.end() );
WRITE_DATA_INT64 ("turn", &larray[0]);
WRITE_DATA_INT64 ("turn", larray.data());
larray.assign (bunchNum_m.begin() + startIdx, bunchNum_m.end() );
WRITE_DATA_INT64 ("bunchNumber", &larray[0]);
WRITE_DATA_INT64 ("bunchNumber", larray.data());
}
++ H5call_m;
......@@ -711,4 +711,4 @@ SetStatistics LossDataSink::computeSetStatistics(unsigned int setIdx) {
stat.rprms_m = stat.rpsum_m * stat.fac_m;
return stat;
}
}
\ No newline at end of file
......@@ -196,6 +196,13 @@ namespace Util {
std::string base64_encode(const std::string &string_to_encode);//unsigned char const* , unsigned int len);
std::string base64_decode(std::string const& s);
template<typename T, typename A>
T* c_data(std::vector<T,A>& v) { return v.empty() ? static_cast<T*>(0) : &(v[0]); }
template<typename T, typename A>
T const* c_data(std::vector<T,A> const& v) { return v.empty() ? static_cast<T const*>(0) : &(v[0]); }
}
template <typename T>
......
......@@ -1007,26 +1007,26 @@ size_t Distribution::getNumberOfParticlesInFile(std::ifstream &inputFile) {
}
void Distribution::createDistributionFromFile(size_t /*numberOfParticles*/, double massIneV) {
// Data input file is only read by node 0.
std::ifstream inputFile;
std::string fileName = Attributes::getString(itsAttr[Attrib::Distribution::FNAME]);
if (!boost::filesystem::exists(fileName)) {
throw OpalException("Distribution::createDistributionFromFile",
"Open file operation failed, please check if \""
+ fileName +
"\" really exists.");
throw OpalException(
"Distribution::createDistributionFromFile",
"Open file operation failed, please check if \"" + fileName + "\" really exists.");
}
if (Ippl::myNode() == 0) {
inputFile.open(fileName.c_str());
}
*gmsg << level3 << "\n"
<< "------------------------------------------------------------------------------------\n";
<< "-------------------------------------------------------------------"
"-----------------\n";
*gmsg << "READ INITIAL DISTRIBUTION FROM FILE \""
<< Attributes::getString(itsAttr[Attrib::Distribution::FNAME])
<< "\"\n";
*gmsg << "------------------------------------------------------------------------------------\n" << endl;
<< Attributes::getString(itsAttr[Attrib::Distribution::FNAME]) << "\"\n";
*gmsg << "-------------------------------------------------------------------"
"-----------------\n"
<< endl;
size_t numberOfParticlesRead = getNumberOfParticlesInFile(inputFile);
/*
......@@ -1037,26 +1037,25 @@ void Distribution::createDistributionFromFile(size_t /*numberOfParticles*/, doub
pmean_m = 0.0;
size_t numPartsToSend = 0;
unsigned int distributeFrequency = 1000;
size_t singleDataSize = (/*sizeof(int) +*/ 6 * sizeof(double));
unsigned int dataSize = distributeFrequency * singleDataSize;
std::vector<char> data;
data.reserve(dataSize);
size_t singleDataSize = 6;
unsigned int dataSize = distributeFrequency * singleDataSize;
std::vector<double> data(dataSize);
const char* buffer;
if (Ippl::myNode() == 0) {
char lineBuffer[1024];
unsigned int numParts = 0;
constexpr unsigned int bufferSize = 1024;
char lineBuffer[bufferSize];