cmtkUniformVolumeMPI.cxx

Go to the documentation of this file.
00001 /*
00002 //
00003 //  Copyright 1997-2010 Torsten Rohlfing
00004 //
00005 //  Copyright 2004-2010 SRI International
00006 //
00007 //  This file is part of the Computational Morphometry Toolkit.
00008 //
00009 //  http://www.nitrc.org/projects/cmtk/
00010 //
00011 //  The Computational Morphometry Toolkit is free software: you can
00012 //  redistribute it and/or modify it under the terms of the GNU General Public
00013 //  License as published by the Free Software Foundation, either version 3 of
00014 //  the License, or (at your option) any later version.
00015 //
00016 //  The Computational Morphometry Toolkit is distributed in the hope that it
00017 //  will be useful, but WITHOUT ANY WARRANTY; without even the implied
00018 //  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019 //  GNU General Public License for more details.
00020 //
00021 //  You should have received a copy of the GNU General Public License along
00022 //  with the Computational Morphometry Toolkit.  If not, see
00023 //  <http://www.gnu.org/licenses/>.
00024 //
00025 //  $Revision: 2398 $
00026 //
00027 //  $LastChangedDate: 2010-10-05 14:54:37 -0700 (Tue, 05 Oct 2010) $
00028 //
00029 //  $LastChangedBy: torstenrohlfing $
00030 //
00031 */
00032 
00033 #include <cmtkconfig.h>
00034 
00035 #ifdef CMTK_BUILD_MPI
00036 
00037 #include <Base/cmtkUniformVolume.h>
00038 
00039 #include <IO/cmtkMPI.h>
00040 
00041 #include <mpi.h>
00042 
00043 namespace cmtk
00044 {
00045 
00048 namespace mpi
00049 {
00050 
00051 template<>
00052 void 
00053 Broadcast
00054 ( MPI::Intracomm& comm, UniformVolume::SmartPtr& inOutPtr, const int root )
00055 {
00056   const size_t msgBufferHdrSize = 
00057     3 * sizeof(int) + // Dims[0..2]
00058     6 * sizeof(int) + // CropRegion.From[0..2], CropRegion.To[0..2]
00059     3 * sizeof( Types::Coordinate ) + // Size[0..2]
00060     3 * sizeof( Types::Coordinate ) + // m_Offset[0..2]
00061     sizeof( ScalarDataType ); // data type
00062 
00063   char msgBufferHdr[ msgBufferHdrSize ];
00064 
00065   if ( comm.Get_rank() == root )
00066     {
00067     // send from this process
00068     int position = 0;
00069     MPI::CHAR.Pack( inOutPtr->GetDims().begin(), 3 * sizeof( inOutPtr->GetDims()[0] ), msgBufferHdr, msgBufferHdrSize, position, comm );
00070 
00071     const cmtk::DataGrid::RegionType& cropRegion = inOutPtr->CropRegion();
00072 
00073     const int cropFrom[3] = { cropRegion.From()[0], cropRegion.From()[1], cropRegion.From()[2] };
00074     const int cropTo[3] = { cropRegion.To()[0], cropRegion.To()[1], cropRegion.To()[2] };
00075     MPI::CHAR.Pack( cropFrom, sizeof( cropFrom ), msgBufferHdr, msgBufferHdrSize, position, comm );
00076     MPI::CHAR.Pack( cropTo, sizeof( cropTo ), msgBufferHdr, msgBufferHdrSize, position, comm );
00077 
00078     MPI::CHAR.Pack( inOutPtr->Size.begin(), 3 * sizeof( inOutPtr->Size[0] ), msgBufferHdr, msgBufferHdrSize, position, comm );
00079 
00080     MPI::CHAR.Pack( inOutPtr->m_Offset.begin(), 3 * sizeof( inOutPtr->m_Offset[0] ), msgBufferHdr, msgBufferHdrSize, position, comm );
00081 
00082     ScalarDataType dataType = TYPE_NONE;
00083     const TypedArray* inData = inOutPtr->GetData();
00084     if ( inData )
00085       {
00086       dataType = inData->GetType();
00087       }
00088     MPI::CHAR.Pack( &dataType, sizeof( dataType ), msgBufferHdr, msgBufferHdrSize, position, comm );
00089     }
00090     
00091   comm.Bcast( msgBufferHdr, msgBufferHdrSize, MPI::CHAR, root );
00092 
00093   if ( comm.Get_rank() != root )
00094     {
00095     // build received volume
00096     int dims[3];
00097     int cropRegionFrom[3];
00098     int cropRegionTo[3];
00099     Types::Coordinate size[3];
00100     Types::Coordinate offset[3];
00101     ScalarDataType dataType;
00102 
00103     int position = 0;
00104     MPI::CHAR.Unpack( msgBufferHdr, msgBufferHdrSize, dims, sizeof( dims ), position, comm );
00105     MPI::CHAR.Unpack( msgBufferHdr, msgBufferHdrSize, cropRegionFrom, sizeof( cropRegionFrom ), position, comm );
00106     MPI::CHAR.Unpack( msgBufferHdr, msgBufferHdrSize, cropRegionTo, sizeof( cropRegionTo ), position, comm );
00107     MPI::CHAR.Unpack( msgBufferHdr, msgBufferHdrSize, size, sizeof( size ), position, comm );
00108     MPI::CHAR.Unpack( msgBufferHdr, msgBufferHdrSize, offset, sizeof( offset ), position, comm );
00109     MPI::CHAR.Unpack( msgBufferHdr, msgBufferHdrSize, &dataType, sizeof( dataType ), position, comm );
00110 
00111     inOutPtr = UniformVolume::SmartPtr( new UniformVolume( UniformVolume::IndexType( dims ), UniformVolume::CoordinateVectorType( size ) ) );
00112     inOutPtr->SetOffset( FixedVector<3,Types::Coordinate>( offset ) );
00113     inOutPtr->CropRegion() = cmtk::DataGrid::RegionType( cmtk::DataGrid::IndexType( cropRegionFrom ), cmtk::DataGrid::IndexType( cropRegionTo ) );
00114     inOutPtr->CreateDataArray( dataType );
00115     }
00116   
00117   comm.Bcast( inOutPtr->GetData()->GetDataPtr(), inOutPtr->GetData()->GetDataSizeBytes(), MPI::CHAR, root );
00118 }
00119 
00120 template void Broadcast<UniformVolume>( ::MPI::Intracomm&, SmartPointer<UniformVolume>&, const int );
00121 
00122 } // namespace mpi
00123 
00125 
00126 } // namespace cmtk
00127 
00128 
00129 #endif // #ifdef CMTK_BUILD_MPI
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines