Equalizer 1.0

eqNBody/channel.cpp

00001 /*
00002  * Copyright (c) 2009, Philippe Robert <philippe.robert@gmail.com> 
00003  *
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions are met:
00006  *
00007  * - Redistributions of source code must retain the above copyright notice, this
00008  *   list of conditions and the following disclaimer.
00009  * - Redistributions in binary form must reproduce the above copyright notice,
00010  *   this list of conditions and the following disclaimer in the documentation
00011  *   and/or other materials provided with the distribution.
00012  * - Neither the name of Eyescale Software GmbH nor the names of its
00013  *   contributors may be used to endorse or promote products derived from this
00014  *   software without specific prior written permission.
00015  *
00016  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00019  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00020  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00021  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00022  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00023  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00024  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00025  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00026  * POSSIBILITY OF SUCH DAMAGE.
00027  */
00028 
00029 #include "channel.h"
00030 
00031 #include "client.h"
00032 #include "initData.h"
00033 #include "config.h"
00034 #include "pipe.h"
00035 
00036 #include "controller.h"
00037 #include "sharedData.h"
00038 
00039 using namespace co::base;
00040 using namespace std;
00041 
00042 namespace eqNbody
00043 {
00044 Channel::Channel( eq::Window* parent ) 
00045     : eq::Channel( parent ) 
00046     , _registerMem( true )
00047     , _mapMem( true )
00048 {
00049     _controller = new Controller( glewGetContext() );
00050 }
00051 
00052 Channel::~Channel()
00053 {
00054     if ( _controller ) 
00055     {
00056         delete _controller;
00057         _controller = 0;
00058     }        
00059 }
00060 
00061 bool Channel::configInit( const eq::uint128_t& initID )
00062 {
00063     if( !eq::Channel::configInit( initID )) 
00064         return false;
00065 
00066     // Initialize the CUDA controller
00067     const InitData& id = static_cast<Config*>( getConfig() )->getInitData();
00068     SharedData& sd = static_cast<Pipe*>( getPipe() )->getSharedData();
00069 
00070     EQCHECK( _controller->init(id, sd.getPos(), true ));
00071     return true;
00072 }
00073 
00074 void Channel::frameDraw( const eq::uint128_t& frameID )
00075 {
00076     const eq::Range& range = getRange();
00077     SharedData& sd = static_cast<Pipe*>( getPipe() )->getSharedData();
00078 
00079     // 1st, register the local memory
00080     if( _registerMem ) 
00081     {
00082         sd.registerMemory( getRange() );
00083         _registerMem = false;
00084 
00085         // Make sure all proxies are mapped before cont'ing
00086         return; 
00087     }
00088 
00089     // 2nd, map remote memory
00090     if( _mapMem ) 
00091     {
00092         sd.mapMemory();
00093         _mapMem = false;
00094     }
00095 
00096     // 3rd, synchronize the shared memory
00097     sd.syncMemory();
00098 
00099     // 4th, update the GPU memory and run one simulation step
00100     const uint32_t nBytes = sd.getNumBytes();
00101     _controller->setArray( BODYSYSTEM_POSITION, sd.getPos(), nBytes );
00102     _controller->setArray( BODYSYSTEM_VELOCITY, sd.getVel(), nBytes );
00103     _controller->compute( sd.getTimeStep(), range );
00104 
00105     // 5th, draw the stars
00106     eq::Channel::frameDraw( frameID );
00107     _controller->draw( sd.getPos(), sd.getCol() );
00108 
00109 #ifndef NDEBUG
00110     outlineViewport();
00111 #endif
00112 
00113     // Finally, redistribute the newly computed data from the GPU to all
00114     // interested mappers
00115     sd.updateMemory( range, _controller );
00116 }
00117 }
Generated on Sun May 8 2011 19:11:05 for Equalizer 1.0 by  doxygen 1.7.3