Esempio n. 1
0
void CProducer::Producing()
{
    while (true)
    {
        int data = GenerateData();

        // writing data
        {
            boost::this_thread::disable_interruption di;
            boost::shared_ptr<boost::mutex> buf = m_bufferLock;
            {
                // ждем освобождения места в буфере
                boost::unique_lock<boost::mutex> lock(*buf);
                while (m_buffer->GetMaxSize() == m_buffer->GetActualSize())
                {
                    m_condition->wait(lock);
                }

                // пишем данные
                {
                    boost::this_thread::sleep(boost::posix_time::milliseconds(m_writeTime));
                    m_buffer->WriteData(data);
                    std::cout << boost::format("Producer write data %1%. In buffer: %2%") % data % m_buffer->GetActualSize() << std::endl;
                }
            }

            // сообщаем, что считали
            m_condition->notify_all();

        }
    }
}
Esempio n. 2
0
	// template method pattern
	void Generator::Generate(Circuit& circuit)
	{
		GenerateData(circuit);
		WriteInputs();
		Show();
		Clear();
	}
bool Test1D_EM()
{
  int dimensionality = 1;

  // Generate some data
  Eigen::MatrixXd data = GenerateData(40, dimensionality);

  // Initialize the model
  std::vector<Model*> models(2);

  for(unsigned int i = 0; i < models.size(); i++)
  {
    Model* model = new GaussianModel(dimensionality);
    models[i] = model;
  }

  MixtureModel mixtureModel;
  mixtureModel.SetModels(models);

  ExpectationMaximization expectationMaximization;
  expectationMaximization.SetData(data);
  expectationMaximization.SetRandom(false);
  expectationMaximization.SetMixtureModel(mixtureModel);
  expectationMaximization.SetMaxIterations(3);
  expectationMaximization.Compute();

  // This is where we got the test output
  MixtureModel finalModel = expectationMaximization.GetMixtureModel();

  for(unsigned int i = 0; i < finalModel.GetNumberOfModels(); ++i)
  {
    std::cout << "Model " << i << ":" << std::endl;
    finalModel.GetModel(i)->Print();
  }

  std::vector<double> testMeans;
  testMeans.push_back(4.90469);
  testMeans.push_back(0.00576815);

  std::vector<double> testVariances;
  testVariances.push_back(0.830928);
  testVariances.push_back(0.862264);

  double epsilon = 1e-4;

  for(unsigned int i = 0; i < finalModel.GetNumberOfModels(); ++i)
  {
    if(fabs(testMeans[i] - finalModel.GetModel(i)->GetMean()(0)) > epsilon)
    {
        return false;
    }

    if(fabs(testVariances[i] - finalModel.GetModel(i)->GetVariance()(0,0)) > epsilon)
    {
        return false;
    }
  }

  return true;
}
Esempio n. 4
0
int main(int, char *[])
{
    XMeansClustering::VectorOfPoints points = GenerateData();
    XMeansClustering xmeans;
    xmeans.SetK(2);
    xmeans.SetPoints(points);
    xmeans.Cluster();

    std::vector<unsigned int> labels = xmeans.GetLabels();

    if(labels[0] != 0 || labels[1] != 0 || labels[2] != 0 ||
            labels[3] != 1 || labels[4] != 1 || labels[5] != 1)
    {
        return EXIT_FAILURE;
    }

    return EXIT_SUCCESS;
}
Esempio n. 5
0
// Make the node work for 1 second
void Node::Work(void)
{
    // Here we must check for any change in node control
    map<int, struct node_control_params>::iterator it = node_control.find(time_elapsed);
    if (it != node_control.end())
    {
        struct node_control_params p = it->second;
        SetRL(p.RL);
        this->active = p.active;
        this->changed = true;
    }

    // Only after changing control we increment time.
    // That way we can let the sensor SendData for the GeneratedData
    // in this method.
    GenerateData();
    time_elapsed++;
}
Esempio n. 6
0
static void DAC_Test()
{

    GenerateData();

    GPIOA* portA = GPIOA::GetInstance();
    portA->EnablePeripheralClock(true);
    portA->SetupGPIO_InAnalog(GPIO_PIN4); //DAC pin

    DAC* dac = DAC::GetInstance();
    dac->EnablePeripheralClock(true);
    DAC_Channel* ch = dac->GetChannel(DAC_CHANNEL_1);
    ch->SelectDataRegister12R();
    ch->SetAmplitude(DAC_CR_AMPL_2047);
    ch->EnableTriangleWaveGeneration();
    ch->EnableChannel();
    //ch->Write

}
// Play a sine wave.
//
// Parameters:
//   out_stream: A pointer to the output audio stream.
//   config: A pointer to struct that contains audio configuration data.
//
//   Returns: An int which has a non-negative number on success.
int PlaySineWave(audio_stream_out_t* out_stream, audio_config_t* config) {
  // Get buffer size and generate data.
  size_t buffer_size = out_stream->common.get_buffer_size(&out_stream->common);
  int num_channels = audio_channel_count_from_out_mask(config->channel_mask);
  uint8_t* data = GenerateData(config->sample_rate, num_channels, buffer_size);

  const size_t kNumBuffersToWrite = 1000;
  int rc = 0;
  // Write kNumBuffersToWrite buffers to the audio hal.
  for (size_t i = 0; i < kNumBuffersToWrite; i++) {
    size_t bytes_wanted =
        out_stream->common.get_buffer_size(&out_stream->common);
    rc = out_stream->write(
        out_stream, data,
        bytes_wanted <= buffer_size ? bytes_wanted : buffer_size);
    if (rc < 0) {
      LOG(ERROR) << "Writing data to hal failed. (" << strerror(rc) << ")";
      break;
    }
  }
  return rc;
}
Esempio n. 8
0
static void DAC_Test_DMA()
{

    GenerateData();

    GPIOA* portA = GPIOA::GetInstance();
    portA->EnablePeripheralClock(true);
    portA->SetupGPIO_InAnalog(GPIO_PIN4); //DAC pin

    DAC* dac = DAC::GetInstance();
    dac->EnablePeripheralClock(true);
    dac->SetIrqHandler(new DAC_IRQ_Handler1());

    DAC_Channel* dacCh = dac->GetChannel(DAC_CHANNEL_1);
    dacCh->SelectTriggerTimer6();
    dacCh->EnableUnderrunInterrupt();
    dacCh->SelectDataRegister12R();

    uint32_t dacDataAddr = (uint32_t) dacCh->GetDataRegisterAddress();

    DMA1* dma1 = DMA1::GetInstance();
    dma1->EnablePeripheralClock(true);
    DMA_Channel* dmaCh3 = dma1->GetChannel(DMA_CHANNEL_3);
    dmaCh3->SetPeripheralAddress(dacDataAddr);
    dmaCh3->SetMemoryAddress((uint32_t) data);
    dmaCh3->SetCircularMode();
    dmaCh3->SetDirection_MemoryToPeripheral();
    dmaCh3->SetMemorySize_16bits();
    dmaCh3->SetPeripheralSize_16bits();
    dmaCh3->SetNumberOfData(nsteps);
    dmaCh3->SetMemoryIncrementMode();
    dmaCh3->SetPriorityHigh();

    dacCh->EnableChannel();
    dacCh->EnableDMA();


}
Esempio n. 9
0
unsigned __stdcall CameraThread( void* Param )
{
    OpData* pInfo = (OpData*) Param;
    OpData* pLocBuff = new OpData( NULL );
#ifdef _SCROUT_
    cout << "CameraThread No." << pInfo->nConv << endl;
#endif
    BOOL bUpdate = FALSE;		//flag used to check if local timer was set by entering critical section
    //DataPack* pLocBuff;		//FrameAnalyzeThread should write info in here
    CvSeq* contour;

    //pInfo->ppCont = &contour;
    pLocBuff->ppCont = &contour;
    pLocBuff->nConv = pInfo->nConv;
    //pInfo->pBuffer = GenerateData( pInfo->nConv );
    pLocBuff->pBuffer = GenerateData( pInfo->nConv );

    if( 1 == pInfo->nConv )
    {
        _beginthreadex( NULL, 0, &FrameCaptureThread, (void*)pLocBuff, 0, NULL );
        _beginthreadex( NULL, 0, &FrameAnalyzeThread, (void*)pLocBuff, 0, NULL );
    }
    //infinite loop of the thread, responsible for generating new local buffers
    while( true )
    {
        if( bInitiated )
        {
            //pInfo->pBuffer = GenerateData( pInfo->nConv );
            pLocBuff->pBuffer = GenerateData( pInfo->nConv );
        }
        //anoter infinite loop - waiting for local timer to be set off
        while( true )
        {
            if( 1 != pInfo->nConv )
            {
                UpdateData( pLocBuff->pBuffer );
            }
            //only after local timer was initiated and buffer hasn't been flushed yet
            if( bInitiated && !pInfo->bDone )
            {
                bUpdate = TryEnterCriticalSection( &csUpdater );
            }
            if( bUpdate )
            {
#ifdef _SCROUT_
                cout << "Updating Local DB(" << pInfo->nConv << ')' << endl;
#endif
                if( 1 != pInfo->nConv )
                {
                    pLocBuff->pBuffer->dPackage.nNum /= 100000;
                }
                pInfo->pBuffer = pLocBuff->pBuffer;		//point pInfo buffer to local, so Local update has access to it
                pInfo->bDone = TRUE;		//info was written to OpData buffer
                ReleaseSemaphore( hLocalCon, 1, NULL );		//tell about it
                bUpdate = FALSE;
                LeaveCriticalSection( &csUpdater );
                break;
            }
        }
    }
    delete pLocBuff;
    /*
    PLACEHOLDER
    */
    return 0;
}
Esempio n. 10
0
/*
   This program is from mpich/tsuite/pt2pt and should be changed there only.
   It needs gcomm and dtype from mpich/tsuite, and can be run with 
   any number of processes > 1.

   This version uses sendrecv and sendrecv_replace (but only in the
   head-to-head mode).
 */
int main( int argc, char **argv )
{
MPI_Datatype *types;
void         **inbufs, **outbufs;
char         **names;
int          *counts, *bytesize, ntype;
MPI_Comm     comms[20];
int          ncomm = 20, rank, np, partner, tag, count;
int          i, j, k, err, world_rank, errloc;
MPI_Status   status;
char         *obuf, *ibuf;

MPI_Init( &argc, &argv );

AllocateForData( &types, &inbufs, &outbufs, &counts, &bytesize, 
		 &names, &ntype );
GenerateData( types, inbufs, outbufs, counts, bytesize, names, &ntype );

MPI_Comm_rank( MPI_COMM_WORLD, &world_rank );
MakeComms( comms, 20, &ncomm, 0 );

/* Test over a wide range of datatypes and communicators */
err = 0;
for (i=0; i<ncomm; i++) {
    MPI_Comm_rank( comms[i], &rank );
    MPI_Comm_size( comms[i], &np );
    if (np < 2) continue;
    tag = i;
    if (rank == 0) 
	partner = np - 1;
    if (rank == np - 1)
	partner = 0;
    for (j=0; j<ntype; j++) {
	if (world_rank == 0) 
	    fprintf( stdout, "Testing type %s\n", names[j] );
        if (rank == 0 || rank == np - 1) {
	    obuf = outbufs[j];
	    for (k=0; k<bytesize[j]; k++) 
		obuf[k] = 0;
	    MPI_Sendrecv( inbufs[j], counts[j], types[j], partner, tag, 
			  outbufs[j], counts[j], types[j], partner, tag, 
			  comms[i], &status );
            /* Test correct */
            MPI_Get_count( &status, types[j], &count );
            if (count != counts[j]) {
		fprintf( stderr, 
			"Error in counts (got %d expected %d) with type %s\n",
			 count, counts[j], names[j] );
                err++;
                }
            if (status.MPI_SOURCE != partner) {
		fprintf( stderr, 
			"Error in source (got %d expected %d) with type %s\n",
			 status.MPI_SOURCE, partner, names[j] );
                err++;
                }
            if ((errloc = CheckData( inbufs[j], outbufs[j], bytesize[j] ))) {
		char *p1, *p2;
		fprintf( stderr, 
                  "Error in data with type %s (type %d on %d) at byte %d\n", 
			 names[j], j, world_rank, errloc - 1 );
		p1 = (char *)inbufs[j];
		p2 = (char *)outbufs[j];
		fprintf( stderr, 
			"Got %x expected %x\n", p1[errloc-1], p2[errloc-1] );
                err++;
                }
	    /* Now do sendrecv_replace */
	    obuf = outbufs[j];
	    ibuf = inbufs[j];
	    for (k=0; k<bytesize[j]; k++) 
		obuf[k] = ibuf[k];
	    /* This would be a better test if the data was different... */
	    MPI_Sendrecv_replace( obuf, counts[j], types[j], partner, tag, 
				  partner, tag, comms[i], &status );
            /* Test correct */
            MPI_Get_count( &status, types[j], &count );
            if (count != counts[j]) {
		fprintf( stderr, 
			"Error in counts (got %d expected %d) with type %s\n",
			 count, counts[j], names[j] );
                err++;
                }
            if (status.MPI_SOURCE != partner) {
		fprintf( stderr, 
			"Error in source (got %d expected %d) with type %s\n",
			 status.MPI_SOURCE, partner, names[j] );
                err++;
                }
            if ((errloc = CheckData( inbufs[j], outbufs[j], bytesize[j] ))) {
		char *p1, *p2;
		fprintf( stderr, 
                  "Error in data with type %s (type %d on %d) at byte %d\n", 
			 names[j], j, world_rank, errloc - 1 );
		p1 = (char *)inbufs[j];
		p2 = (char *)outbufs[j];
		fprintf( stderr, 
			"Got %x expected %x\n", p1[errloc-1], p2[errloc-1] );
                err++;
                }
            }
	}
    }
if (err > 0) {
    fprintf( stderr, "%d errors on %d\n", err, rank );
    }
FreeDatatypes( types, inbufs, outbufs, counts, bytesize, names, ntype );
FreeComms( comms, ncomm );
MPI_Finalize();
return err;
}
Esempio n. 11
0
int main( int argc, char **argv )
{
MPI_Datatype *types;
void         **inbufs, **outbufs;
char         **names;
int          *counts, *bytesize, ntype;
MPI_Comm     comms[20];
int          ncomm = 20, rank, np, partner, tag;
int          i, j, k, err, toterr, world_rank;
MPI_Status   status, statuses[2];
int          flag;
char         *obuf;
MPI_Request  requests[2];


MPI_Init( &argc, &argv );

AllocateForData( &types, &inbufs, &outbufs, &counts, &bytesize, 
		 &names, &ntype );
GenerateData( types, inbufs, outbufs, counts, bytesize, names, &ntype );

MPI_Comm_rank( MPI_COMM_WORLD, &world_rank );
MakeComms( comms, 20, &ncomm, 0 );

/* Test over a wide range of datatypes and communicators */
err = 0;
for (i=0; i<ncomm; i++) {
    MPI_Comm_rank( comms[i], &rank );
    MPI_Comm_size( comms[i], &np );
    if (np < 2) continue;
    tag = i;
    /* This is the test.  
       master:                               worker:
       irecv                                 send
       isend
       testall  (fail)
       sendrecv                              sendrecv
                                             irecv
       sendrecv                              sendrecv
                                             wait
       sendrecv                              sendrecv
       testall  (should succeed)                  
     */
    for (j=0; j<ntype; j++) {
	if (world_rank == 0 && verbose) 
	    fprintf( stdout, "Testing type %s\n", names[j] );
        if (rank == 0) {
	    /* Master */
	    partner = np - 1;
#if 0
	    MPIR_PrintDatatypePack( stdout, counts[j], types[j], 0, 0 );
#endif
	    obuf = outbufs[j];
	    for (k=0; k<bytesize[j]; k++) 
		obuf[k] = 0;
	    
	    MPI_Irecv(outbufs[j], counts[j], types[j], partner, tag, 
		      comms[i], &requests[0] );
	    
	    /* Use issend to ensure that the test cannot complete */
	    MPI_Isend( inbufs[j], counts[j], types[j], partner, tag, 
		        comms[i], &requests[1] );

	    /* Note that the send may have completed */
	    MPI_Testall( 2, &requests[0], &flag, statuses );
	    if (flag) {
		err++;
		fprintf( stderr, "MPI_Testall returned flag == true!\n" );
		}
	    if (requests[1] == MPI_REQUEST_NULL) {
		err++;
		fprintf( stderr, "MPI_Testall freed a request\n" );
		}
	    MPI_Sendrecv( MPI_BOTTOM, 0, MPI_INT, partner, ncomm+i, 
			  MPI_BOTTOM, 0, MPI_INT, partner, ncomm+i, 
			  comms[i], &status );
	    MPI_Sendrecv( MPI_BOTTOM, 0, MPI_INT, partner, ncomm+i, 
			  MPI_BOTTOM, 0, MPI_INT, partner, ncomm+i, 
			  comms[i], &status );
	    MPI_Sendrecv( MPI_BOTTOM, 0, MPI_INT, partner, ncomm+i, 
			  MPI_BOTTOM, 0, MPI_INT, partner, ncomm+i, 
			  comms[i], &status );
	    /* This should succeed, but may fail if the wait below is 
	       still waiting */
	    MPI_Testall( 2, requests, &flag, statuses );
	    if (!flag) {
		err++;
		fprintf( stderr, "MPI_Testall returned flag == false!\n" );
		}
	    if (requests[0] != MPI_REQUEST_NULL || 
		requests[1] != MPI_REQUEST_NULL) {
		err++;
		fprintf( stderr, "MPI_Testall failed to free requests (test %d)\n", j );
		if (requests[0] != MPI_REQUEST_NULL) {
		    fprintf( stderr, "Failed to free Irecv request\n" );
		}
		if (requests[1] != MPI_REQUEST_NULL) {
		    fprintf( stderr, "Failed to free Isend request\n" );
		}
	    }
	    /* Check the received data */
            if (CheckDataAndPrint( inbufs[j], outbufs[j], bytesize[j],
				   names[j], j )) {
		err++;
		}
	    }
	else if (rank == np - 1) {
	    /* receiver */
	    partner = 0;
	    obuf = outbufs[j];
	    for (k=0; k<bytesize[j]; k++) 
		obuf[k] = 0;
	    
	    MPI_Send( inbufs[j], counts[j], types[j], partner, tag, 
		        comms[i] );
	    
	    MPI_Sendrecv( MPI_BOTTOM, 0, MPI_INT, partner, ncomm+i, 
			  MPI_BOTTOM, 0, MPI_INT, partner, ncomm+i, 
			  comms[i], &status );

	    MPI_Irecv(outbufs[j], counts[j], types[j], partner, tag, 
		      comms[i], &requests[0] );

	    MPI_Sendrecv( MPI_BOTTOM, 0, MPI_INT, partner, ncomm+i, 
			  MPI_BOTTOM, 0, MPI_INT, partner, ncomm+i, 
			  comms[i], &status );

	    MPI_Wait( requests, statuses );
            if (CheckDataAndPrint( inbufs[j], outbufs[j], bytesize[j],
				   names[j], j )) {
                err++;
		}
	    MPI_Sendrecv( MPI_BOTTOM, 0, MPI_INT, partner, ncomm+i, 
			  MPI_BOTTOM, 0, MPI_INT, partner, ncomm+i, 
			  comms[i], &status );
	    }
	}
    }

if (err > 0) {
    fprintf( stderr, "%d errors on %d\n", err, rank );
    }
MPI_Allreduce( &err, &toterr, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD );
 if (world_rank == 0) {
     if (toterr == 0) {
	 printf( " No Errors\n" );
     }
     else {
	 printf (" Found %d errors\n", toterr );
     }
 }
FreeDatatypes( types, inbufs, outbufs, counts, bytesize, names, ntype );
FreeComms( comms, ncomm );
MPI_Finalize();

return err;
}
bool Test2D_EM()
{
  int dimensionality = 2;

  // Generate some data
  Eigen::MatrixXd data = GenerateData(40, dimensionality);

  // Initialize the model
  std::vector<Model*> models(2);

  for(unsigned int i = 0; i < models.size(); i++)
  {
    Model* model = new GaussianModel(dimensionality);
    models[i] = model;
  }

  MixtureModel mixtureModel;
  mixtureModel.SetModels(models);

  ExpectationMaximization expectationMaximization;
  expectationMaximization.SetData(data);
  expectationMaximization.SetRandom(false);
  expectationMaximization.SetMixtureModel(mixtureModel);
  expectationMaximization.SetMaxIterations(3);
  expectationMaximization.SetInitializationTechniqueToKMeans();
  expectationMaximization.Compute();

  // This is where we got the test output
  MixtureModel finalModel = expectationMaximization.GetMixtureModel();
  for(unsigned int i = 0; i < finalModel.GetNumberOfModels(); ++i)
  {
    std::cout << "Model " << i << ":" << std::endl;
    finalModel.GetModel(i)->Print();
  }

  Eigen::VectorXd mean0(2);
  mean0 << 0.0631675, -0.147669;

  Eigen::VectorXd mean1(2);
  mean1 << 10.23, 10.069;

  Eigen::MatrixXd var0(2,2);
  var0 << 0.818243, -0.027182,
          -0.027182,  0.836947;

  Eigen::MatrixXd var1(2,2);
  var1 << 2.49997, 0.10499,
          0.10499, 1.85563;

  double epsilon = 1e-4;

  // Check means
  if((finalModel.GetModel(0)->GetMean() - mean0).norm() > epsilon)
  {
      return false;
  }
  if((finalModel.GetModel(1)->GetMean() - mean1).norm() > epsilon)
  {
      return false;
  }

  // Check variances
  if((finalModel.GetModel(0)->GetVariance() - var0).norm() > epsilon)
  {
      return false;
  }
  if((finalModel.GetModel(1)->GetVariance() - var1).norm() > epsilon)
  {
      return false;
  }

  return true;
}
Esempio n. 13
0
/*
   This program is from mpich/tsuite/pt2pt and should be changed there only.
   It needs gcomm and dtype from mpich/tsuite, and can be run with 
   any number of processes > 1.

   This version uses Pack to send a message and Unpack OR the datatype 
   to receive it.
 */
int main( int argc, char **argv )
{
MPI_Datatype *types;
void         **inbufs, **outbufs;
char         **names;
char         *packbuf, *unpackbuf;
int          packsize, unpacksize, position;
int          *counts, *bytesize, ntype;
MPI_Comm     comms[20];
int          ncomm = 20, rank, np, partner, tag, count;
int          i, j, k, err, toterr, world_rank;
int          errloc;
MPI_Status   status;
char         *obuf;

MPI_Init( &argc, &argv );

AllocateForData( &types, &inbufs, &outbufs, &counts, &bytesize, 
		 &names, &ntype );
GenerateData( types, inbufs, outbufs, counts, bytesize, names, &ntype );

MPI_Comm_rank( MPI_COMM_WORLD, &world_rank );
MakeComms( comms, 20, &ncomm, 0 );

/* Test over a wide range of datatypes and communicators */
err = 0;
for (i=0; i<ncomm; i++) {
    MPI_Comm_rank( comms[i], &rank );
    MPI_Comm_size( comms[i], &np );
    if (np < 2) continue;
    if (world_rank == 0 && verbose) {
	fprintf( stdout, "Testing with communicator with %d members\n", np );
	}
    tag = i;
    for (j=0; j<ntype; j++) {
	if (world_rank == 0 && verbose) 
	    fprintf( stdout, "Testing type %s\n", names[j] );
        if (rank == 0) {
	    partner = np - 1;
	    MPI_Pack_size( counts[j], types[j], comms[i], &packsize );
	    packbuf = (char *)malloc( packsize );
		if (!packbuf) {
			fprintf( stderr, "[%i] Aborting\n",rank );fflush(stderr);
			MPI_Abort( MPI_COMM_WORLD, 1 );
		}
	    position = 0;
	    MPI_Pack( inbufs[j], counts[j], types[j], packbuf, packsize, 
		      &position, comms[i] );
	    /* Send twice */
            MPI_Send( packbuf, position, MPI_PACKED, partner, tag, comms[i] );
            MPI_Send( packbuf, position, MPI_PACKED, partner, tag, comms[i] );
	    free( packbuf );
            }
        else if (rank == np-1) {
	    partner = 0;
	    obuf = outbufs[j];
	    for (k=0; k<bytesize[j]; k++) 
		obuf[k] = 0;
	    /* Receive directly */
            MPI_Recv( outbufs[j], counts[j], types[j], partner, tag, comms[i],
                      &status );
            /* Test correct */
            MPI_Get_count( &status, types[j], &count );
            if (count != counts[j]) {
		fprintf( stderr, 
			"Error in counts (got %d expected %d) with type %s\n",
			 count, counts[j], names[j] );
                err++;
                }
            if (status.MPI_SOURCE != partner) {
		fprintf( stderr, 
			"Error in source (got %d expected %d) with type %s\n",
			 status.MPI_SOURCE, partner, names[j] );
                err++;
                }
            if ((errloc = CheckData( inbufs[j], outbufs[j], bytesize[j] ))) {
		fprintf( stderr, 
                    "Error in data at byte %d with type %s (type %d on %d)\n", 
			 errloc - 1, names[j], j, world_rank );
                err++;
                }
	    /* Receive packed, then unpack */
	    MPI_Pack_size( counts[j], types[j], comms[i], &unpacksize ); 
	    unpackbuf = (char *)malloc( unpacksize );
		if (!unpackbuf) {
			fprintf( stderr, "[%i] Aborting\n",rank );fflush(stderr);
			MPI_Abort( MPI_COMM_WORLD, 1 );
		}
            MPI_Recv( unpackbuf, unpacksize, MPI_PACKED, partner, tag, 
		      comms[i], &status );
	    obuf = outbufs[j];
	    for (k=0; k<bytesize[j]; k++) 
		obuf[k] = 0;
	    position = 0;
            MPI_Get_count( &status, MPI_PACKED, &unpacksize );
	    MPI_Unpack( unpackbuf, unpacksize, &position, 
		        outbufs[j], counts[j], types[j], comms[i] );
	    free( unpackbuf );
            /* Test correct */
#ifdef FOO
	    /* Length is tricky; a correct code will have signaled an error 
	       in MPI_Unpack */
	    count = position;
            if (count != counts[j]) {
		fprintf( stderr, 
		"Error in counts (got %d expected %d) with type %s (Unpack)\n",
			 count, counts[j], names[j] );
                err++;
                }
#endif
            if (status.MPI_SOURCE != partner) {
		fprintf( stderr, 
		"Error in source (got %d expected %d) with type %s (Unpack)\n",
			 status.MPI_SOURCE, partner, names[j] );
                err++;
                }
            if ((errloc = CheckData( inbufs[j], outbufs[j], bytesize[j] ))) {
		fprintf( stderr, 
            "Error in data at byte %d with type %s (type %d on %d, Unpack)\n", 
			errloc - 1, names[j], j, world_rank );
                err++;
                }
            }
	}
    }
if (err > 0) {
    fprintf( stderr, "%d errors on %d\n", err, rank );
    }
MPI_Allreduce( &err, &toterr, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD );
 if (world_rank == 0) {
     if (toterr == 0) {
	 printf( " No Errors\n" );
     }
     else {
	 printf (" Found %d errors\n", toterr );
     }
 }
FreeDatatypes( types, inbufs, outbufs, counts, bytesize, names, ntype );
FreeComms( comms, ncomm );
MPI_Barrier( MPI_COMM_WORLD );
MPI_Finalize();
return err;
}
Esempio n. 14
0
int main( int argc, char **argv )
{
    MPI_Datatype *types;
    void         **inbufs, **outbufs;
    char         **names;
    int          *counts, *bytesize, ntype;
    MPI_Comm     comms[20];
    int          ncomm = 20, rank, np, partner, tag;
    int          i, j, k, err, toterr, world_rank, errloc;
    MPI_Status   status, statuses[2];
    int          flag, index;
    char         *obuf;
    MPI_Request  requests[2];


    MPI_Init( &argc, &argv );

    AllocateForData( &types, &inbufs, &outbufs, &counts, &bytesize, 
		     &names, &ntype );
    GenerateData( types, inbufs, outbufs, counts, bytesize, names, &ntype );

    MPI_Comm_rank( MPI_COMM_WORLD, &world_rank );
    MakeComms( comms, 20, &ncomm, 0 );

	
/* Test over a wide range of datatypes and communicators */
    err = 0;
    for (i=0; i<ncomm; i++) {
	MPI_Comm_rank( comms[i], &rank );
	MPI_Comm_size( comms[i], &np );
	if (np < 2) continue;
	tag = i;
	for (j=0; j<ntype; j++) {
		if (world_rank == 0){ 
/* SI make size of outputindependent of number of processes */
		  if (i<2) fprintf( stdout, "Testing type %s\n",names[j] );
		}
	    /* This test does an irsend between both partners, with 
	       a sendrecv after the irecv used to guarentee that the
	       irsend has a matching receive
	       */
	    if (rank == 0) {
		partner = np - 1;
#if 0
		MPIR_PrintDatatypePack( stdout, counts[j], types[j], 0, 0 );
#endif
		obuf = outbufs[j];
		for (k=0; k<bytesize[j]; k++) 
		    obuf[k] = 0;
	    
		MPI_Irecv(outbufs[j], counts[j], types[j], partner, tag, 
			  comms[i], &requests[0] );

		MPI_Sendrecv( MPI_BOTTOM, 0, MPI_INT, partner, ncomm+i, 
			      MPI_BOTTOM, 0, MPI_INT, partner, ncomm+i, 
			      comms[i], &status );

		MPI_Irsend( inbufs[j], counts[j], types[j], partner, tag, 
			    comms[i], &requests[1] );
	    
		do {
		    MPI_Waitany( 2, requests, &index, &status );
		} while (index != 0);

		/* Always the possiblity that the Irsend is still waiting */
		MPI_Waitall( 2, requests, statuses );
		if ((errloc = CheckData( inbufs[j], outbufs[j], bytesize[j] ))) {
		    char *p1, *p2;
		    fprintf( stderr, 
			     "Error in data with type %s (type %d on %d) at byte %d\n", 
			     names[j], j, world_rank, errloc - 1 );
		    p1 = (char *)inbufs[j];
		    p2 = (char *)outbufs[j];
		    fprintf( stderr, 
			     "Got %x expected %x\n", p1[errloc-1], p2[errloc-1] );
		    err++;
#if 0
		    MPIR_PrintDatatypeUnpack( stderr, counts[j], types[j], 
					      0, 0 );
#endif
		}
	    }
	    else if (rank == np - 1) {
		partner = 0;
		obuf = outbufs[j];
		for (k=0; k<bytesize[j]; k++) 
		    obuf[k] = 0;
	    
		MPI_Irecv(outbufs[j], counts[j], types[j], partner, tag, 
			  comms[i], &requests[0] );

		MPI_Sendrecv( MPI_BOTTOM, 0, MPI_INT, partner, ncomm+i, 
			      MPI_BOTTOM, 0, MPI_INT, partner, ncomm+i, 
			      comms[i], &status );

		/* Wait for irecv to complete */
		do {
		    MPI_Test( &requests[0], &flag, &status );
		} while (!flag);
		if ((errloc = CheckData( inbufs[j], outbufs[j], bytesize[j] ))) {
		    char *p1, *p2;
		    fprintf( stderr, 
			     "Error in data with type %s (type %d on %d) at byte %d\n", 
			     names[j], j, world_rank, errloc - 1 );
		    p1 = (char *)inbufs[j];
		    p2 = (char *)outbufs[j];
		    fprintf( stderr, 
			     "Got %x expected %x\n", p1[errloc-1], p2[errloc-1] );
		    err++;
#if 0
		    MPIR_PrintDatatypeUnpack( stderr, counts[j], types[j], 
					      0, 0 );
#endif
		}

		MPI_Irsend( inbufs[j], counts[j], types[j], partner, tag, 
			    comms[i], &requests[1] );
	    
		MPI_Waitall(1, &requests[1], &status );
	    }
	}
    }

    if (err > 0) {
	fprintf( stderr, "%d errors on %d\n", err, rank );
    }
    MPI_Allreduce( &err, &toterr, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD );
    if (world_rank == 0) {
	if (toterr == 0) {
	    printf( " No Errors\n" );
	}
	else {
	    printf (" Found %d errors\n", toterr );
	}
    }
    FreeDatatypes( types, inbufs, outbufs, counts, bytesize, names, ntype );
    FreeComms( comms, ncomm );
    MPI_Finalize();

    return err;
}
Esempio n. 15
0
/*
   This program is from mpich/tsuite/pt2pt and should be changed there only.
   It needs gcomm and dtype from mpich/tsuite, and can be run with 
   any number of processes > 1.
 */
int main( int argc, char **argv)
{
    MPI_Datatype *types;
    void         **inbufs, **outbufs;
    char         **names;
    int          *counts, *bytesize, ntype;
    MPI_Comm     comms[20];
    int          ncomm = 20, rank, np, partner, tag, count;
    int          i, j, k, err, toterr, world_rank, errloc;
    MPI_Status   status;
    char         *obuf;

    MPI_Init( &argc, &argv );
    
    /* 
     * Check for -basiconly to select only the simple datatypes
     */
    for (i=1; i<argc; i++) {
	if (!argv[i]) break;
	if (strcmp( argv[i], "-basiconly" ) == 0) {
	    BasicDatatypesOnly();
	}
	else if (strcmp( argv[i], "-verbose" ) == 0) {
	    verbose = 1;
	}
    }
    
    AllocateForData( &types, &inbufs, &outbufs, &counts, &bytesize, 
		     &names, &ntype );
    GenerateData( types, inbufs, outbufs, counts, bytesize, names, &ntype );
    
    MPI_Comm_rank( MPI_COMM_WORLD, &world_rank );
    MakeComms( comms, 20, &ncomm, 0 );
    
 /* Test over a wide range of datatypes and communicators */
    err = 0;
    for (i=0; i<ncomm; i++) {
	if (comms[i] == MPI_COMM_NULL) continue;
	MPI_Comm_rank( comms[i], &rank );
	MPI_Comm_size( comms[i], &np );
	if (np < 2) continue;
	if (world_rank == 0 && verbose) 
	    fprintf( stdout, "Testing communicator number %d\n", i );
	
	tag = i;
	for (j=0; j<ntype; j++) {
	//for (j=52; j<53; j++) {
	    if (world_rank == 0 && verbose) 
		fprintf( stdout, "Testing type %s\n", names[j] );
	    if (rank == 0) {
		partner = np - 1;
#if 0
		MPIR_PrintDatatypePack( stdout, counts[j], types[j], 0, 0 );
#endif
		MPI_Send( inbufs[j], counts[j], types[j], partner, tag, comms[i] );
            }
	    else if (rank == np-1) {
		partner = 0;
		obuf = outbufs[j];
		for (k=0; k<bytesize[j]; k++) 
		    obuf[k] = 0;
		MPI_Recv( outbufs[j], counts[j], types[j], partner, tag, 
			  comms[i], &status );
		/* Test correct */
		MPI_Get_count( &status, types[j], &count );
		if (count != counts[j]) {
		    fprintf( stderr, 
		     "Error in counts (got %d expected %d) with type %s\n",
			 count, counts[j], names[j] );
		    err++;
                }
		if (status.MPI_SOURCE != partner) {
		    fprintf( stderr, 
			"Error in source (got %d expected %d) with type %s\n",
			 status.MPI_SOURCE, partner, names[j] );
		    err++;
                }
		if ((errloc = CheckData( inbufs[j], outbufs[j], bytesize[j] ))) {
		    char *p1, *p2;
                    int *q1, *q2;
                    d1 *base;
		    fprintf( stderr, 
		    "Error in data with type %s (type %d on %d) at byte %d\n", 
			     names[j], j, world_rank, errloc - 1 );
		    p1 = (char*)inbufs[j];
                    q1 = (int*)inbufs[j];
		    p2 = (char*)outbufs[j];
                    q2 = (int*)outbufs[j];
		    fprintf( stderr, 
			"Got %x expected %x\n", p1[errloc-1], p2[errloc-1] );

                    base = (d1*)inbufs[j];
                    for (k=0; k<10; k++) {
                        if (k%60 == 0) printf("\n");
                        printf("%d %lf ", base[k].a1, base[k].a2);
                    }
                    /*
                    for (k=0; k<bytesize[j]/sizeof(int); ++k) {
                        if (k%60 == 0) printf("\n");
                        printf("%d ", q1[k]);
                    }
                    */
                    base = (d1*)outbufs[j];
                    for (k=0; k<10; k++) {
                        if (k%60 == 0) printf("\n");
                        printf("%d %lf ", base[k].a1, base[k].a2);
                    }
                    /*
                    for (k=0; k<bytesize[j]/sizeof(int); ++k) {
                        if (k%60 == 0) printf("\n");
                        printf("%d ", q2[k]);
                    }
                    */
                    printf("\n");
		    err++;
#if 0
		    MPIR_PrintDatatypeUnpack( stderr, counts[j], types[j], 
					      0, 0 );
#endif
                }
            }
	}
    }
    if (err > 0) {
	fprintf( stderr, "%d errors on %d\n", err, rank );
    }
    MPI_Allreduce( &err, &toterr, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD );
    if (world_rank == 0) {
	if (toterr == 0) {
	    printf( " No Errors\n" );
	}
	else {
	    printf (" Found %d errors\n", toterr );
	}
    }
    FreeDatatypes( types, inbufs, outbufs, counts, bytesize, names, ntype );
    FreeComms( comms, ncomm );
    MPI_Finalize();
    return err;
}