示例#1
0
/// sequentially read nxn array in row major order
void sequentialRead()
{
   MDArray *array = new MDArray(fileName);
   PagedStorageContainer::resetPerfCounts();
   gettimeofday(&tim, NULL);
   t1 = tim.tv_sec + tim.tv_usec/1000000.0;
   MDIterator *it = array->createIterator(Dense, row);
   while (it->moveNext())
   {
      it->get(coord, datum);
   }
   delete it;
   delete array;

   gettimeofday(&tim, NULL);
   t2 = tim.tv_sec + tim.tv_usec/1000000.0;
   readCount += PagedStorageContainer::readCount;
   writeCount += PagedStorageContainer::writeCount;
   accessTime += PagedStorageContainer::accessTime;

   execTime += (t2 - t1);

   fprintf(pFile, "%-14s %8i %8i %11.3f %11.3f\n", "seq read", PagedStorageContainer::readCount, PagedStorageContainer::writeCount, PagedStorageContainer::accessTime, t2-t1);

}
示例#2
0
    void StringFunction::operator()(MDArray& input_phy_points, MDArray& output_values,
                                    const stk::mesh::Bucket& bucket, const MDArray& parametric_coordinates, double time_value_optional) 
    {
      PRINT("tmp srk StringFunction::operator(bucket) getName()= " << getName() << " input_phy_points= " << input_phy_points << " output_values= " << output_values);

      VERIFY_OP(input_phy_points.rank(), ==, 3, "StringFunction::operator() must pass in input_phy_points(numCells, numPointsPerCell, spaceDim)");
      int nPoints = input_phy_points.dimension(1);
      int nSpaceDim = input_phy_points.dimension(2);
      int nOutDim = output_values.dimension(2);
      MDArray input_phy_points_one(1, nPoints, nSpaceDim);
      MDArray output_values_one   (1, nPoints, nOutDim);
      const unsigned num_elements_in_bucket = bucket.size();
      for (unsigned iElement = 0; iElement < num_elements_in_bucket; iElement++)
        {
          stk::mesh::Entity& element = bucket[iElement];
          for (int iPoint = 0; iPoint<nPoints; iPoint++)
            {
              for (int iSpaceDim=0; iSpaceDim < nSpaceDim; iSpaceDim++)
                {
                  input_phy_points_one(0, iPoint, iSpaceDim) = input_phy_points(iElement, iPoint, iSpaceDim);
                }
            }
          (*this)(input_phy_points_one, output_values_one, element, parametric_coordinates, time_value_optional);
          for (int iPoint = 0; iPoint<nPoints; iPoint++)
            {
              for (int iDOF = 0; iDOF < nOutDim; iDOF++)
                {
                  output_values(iElement, iPoint, iDOF) = output_values_one(0, iPoint, iDOF);
                }
            }
        }
    }
示例#3
0
/// strided insertion, using the iterator
void stridedIteratorInsert()
{
    MDArray *array;
    Btree::MSplitter<Datum_t> leafSp;
    Btree::MSplitter<PID_t> intSp;
    if (type == DMA)
        array = new MDArray(dim, type, row, fileName);
    else if (type == BTREE)
        array = new MDArray(dim, row, &leafSp, &intSp, fileName);
   PagedStorageContainer::resetPerfCounts();
   
   gettimeofday(&tim, NULL);
   t1 = tim.tv_sec + tim.tv_usec/1000000.0;
   MDIterator *it = array->createIterator(Dense, col);
   while (it->moveNext())
   {
      it->put(12345);
   }
   delete it;
   delete array;

   gettimeofday(&tim, NULL);
   t2 = tim.tv_sec + tim.tv_usec/1000000.0;
   readCount += PagedStorageContainer::readCount;
   writeCount += PagedStorageContainer::writeCount;
   accessTime += PagedStorageContainer::accessTime;
   
   execTime += (t2 - t1);

   fprintf(pFile, "%-14s %8i %8i %11.3f %11.3f\n", "batch stride", PagedStorageContainer::readCount, PagedStorageContainer::writeCount, PagedStorageContainer::accessTime, t2-t1);

}
示例#4
0
/// strided insertion into nxn array (insert going down columns first into row
//major array
void stridedInsert()
{
    MDArray *array;
    Btree::MSplitter<Datum_t> leafSp;
    Btree::MSplitter<PID_t> intSp;
    if (type == DMA)
        array = new MDArray(dim, type, row, fileName);
    else if (type == BTREE)
        array = new MDArray(dim, row, &leafSp, &intSp, fileName);
   PagedStorageContainer::resetPerfCounts();

   gettimeofday(&tim, NULL);
   t1 = tim.tv_sec + tim.tv_usec/1000000.0;
   for (i64 j = 0; j < n; j++)
   {
      for (i64 i = 0; i < n; i++)
      {
         coord = MDCoord(2, i, j);
         array->put(coord, 12345);
      }
   }
   delete array;

   gettimeofday(&tim, NULL);
   t2 = tim.tv_sec + tim.tv_usec/1000000.0;
   readCount += PagedStorageContainer::readCount;
   writeCount += PagedStorageContainer::writeCount;
   accessTime += PagedStorageContainer::accessTime;
   execTime += (t2 - t1);

   fprintf(pFile, "%-14s %8i %8i %11.3f %11.3f\n", "stride insert", PagedStorageContainer::readCount, PagedStorageContainer::writeCount, PagedStorageContainer::accessTime, t2-t1);
   
}
示例#5
0
    ///  Dimensions of parametric_coordinates and input_phy_points are required to be ([P],[D])
    /// output_values: ([P], [DOF])
    void StringFunction::operator()(MDArray& input_phy_points, MDArray& output_values,
                                    const stk::mesh::Entity& element, const MDArray& parametric_coordinates, double time_value_optional) 
    {
      PRINT("tmp srk StringFunction::operator(element) getName()= " << getName() << " input_phy_points= " << input_phy_points << " output_values= " << output_values);

      argsAreValid(input_phy_points, output_values);
      argsAreValid(parametric_coordinates, output_values);

      m_element = &element;
      m_have_element = true;
      VERIFY_OP(parametric_coordinates.rank(), ==, 2, "StringFunction::operator() parametric_coordinates rank bad");
      m_parametric_coordinates = MDArray(1, parametric_coordinates.dimension(1));
      int nPoints=parametric_coordinates.dimension(0);
      int spaceDim = parametric_coordinates.dimension(1);
      for (int iPoint = 0; iPoint < nPoints; iPoint++)
        {
          for (int iSpace=0; iSpace < spaceDim; iSpace++)
            m_parametric_coordinates(0, iSpace)=parametric_coordinates(iPoint, iSpace);
          (*this)(input_phy_points, output_values, time_value_optional);
        }

      // reset this else we won't be able to reuse this object correctly
      m_have_element = false;
      m_element = 0;

      // *  Dimensions of parametric_coordinates are required to be ([P],[D])
      //FieldFunction:: void operator()(const stk::mesh::Entity *element, const MDArray& parametric_coordinates, MDArray& out);

    }
示例#6
0
/// random read from nxn array. all indices are read once
void randomRead()
{
   i64 k[n*n];
   for (int i = 0; i < n*n; i++)
   {
      k[i] = i;
   }
   MDArray *array = new MDArray(fileName);
   PagedStorageContainer::resetPerfCounts();
   permute(k, n*n);

   gettimeofday(&tim, NULL);
   t1 = tim.tv_sec + tim.tv_usec/1000000.0;
   for (int i = 0; i < n*n; i++)
   {
      coord = MDCoord(2, k[i]/n, k[i]%n);
      array->get(coord, datum);
   }
   delete array;

   gettimeofday(&tim, NULL);
   t2 = tim.tv_sec + tim.tv_usec/1000000.0;
   readCount += PagedStorageContainer::readCount;
   writeCount += PagedStorageContainer::writeCount;
   accessTime += PagedStorageContainer::accessTime;
   execTime += (t2 - t1);

   fprintf(pFile, "%-14s %8i %8i %11.3f %11.3f\n", "rand read", PagedStorageContainer::readCount, PagedStorageContainer::writeCount, PagedStorageContainer::accessTime, t2-t1);

}
示例#7
0
void MultinomialDensities::estimate(vector<MDArray<double> > & ess)
{
	assert(!ess.empty());

    MDArray<double> e = ess[0];
    e.add_inplace(pseudo_count);
	set_parameters(e);
}
示例#8
0
TEST(MDArray, Read)
{
    i64 rows = 20L;
    i64 cols = 20L;
    MDCoord dim(2, rows, cols);
    StorageType type = DMA;
    i64 blockDims[] = {10L, 10L};
    u8 blockOrders[] = {0, 1};
    u8 microOrders[] = {0, 1};
    //permute(blockOrders, dim.nDim);
    //permute(microOrders, dim.nDim);
    Linearization *block = new BlockBased(dim.nDim, dim.coords, blockDims,
                                          blockOrders, microOrders);
    const char *fileName = "test1.bin";
    MDCoord key;
    Datum_t datum = 1.0;

    MDArray *array = new MDArray(dim, type, block, fileName);
    
    MDIterator *it = array->createIterator(Dense, block);

    srand(time(NULL));
    const int num=100;
    std::map<int, MDCoord> keys;
    std::map<int, Datum_t> data;
    int total = rows*cols;
    for(int i=0; i<num; i++) {
        int k = rand() % total;
        if (data.find(k) == data.end()) {
            data[k] = rand();
            it->setIndexRange(k, k+1);
            it->moveNext();
            it->put(data[k]);
            MDCoord key;
            it->get(key, datum);
            keys[k] = key;
        }
        else
            i--;
    }
    
    delete it;

    delete array;

    array = new MDArray(fileName);
    Datum_t datum1;
    for (std::map<int,MDCoord>::iterator s = keys.begin();
         s != keys.end();
         ++s) {
        //cout<<"checking "<<s->second.toString()<<endl;
        array->get(s->second, datum1);
        ASSERT_DOUBLE_EQ(data[s->first], datum1);
    }
    delete array;
    
    delete block;
}
示例#9
0
int main(int argc, char **argv)
{
	const int required = 2;
	char fileName[100] = "/riot/mb";
	unsigned int tm;
	if (argc >= required+1)
		tm = atoi(argv[required]);
	else
		tm = time(NULL);
	srand(tm);
	cerr<<"seed = "<<tm<<endl;
	if (argc < required) {
		cerr<<"Usage: "<<argv[0]<<"<splitter type>"<<endl
			<<"splitter type: M,A,D"<<endl;
		return 0;
	}

	char splitterType = argv[1][0];

    i64 arraydims[] = {20000,20000};
    i64 blockdims[] = {31,31};
    u8 orders[] = {0, 1};
    Linearization<2> *lin = new BlockBased<2>(arraydims, blockdims, orders,
                                              orders);
    MDArray<2> *array;
    StorageParam sp;
    sp.fileName = fileName;
    sp.intSp = 'M';
    sp.leafSp = splitterType;
    if (splitterType == 'D') {
        // dma
        sp.type = DMA;
    }
    else if (splitterType == 'M') {
        sp.type = BTREE;
        sp.useDenseLeaf = false;
    }
    else if (splitterType == 'A') {
        sp.type = BTREE;
        sp.useDenseLeaf = true;
    }
    else {
        cerr<<"wrong splitter type"<<endl;
        exit(1);
    }
    
    array = new MDArray<2>(&sp, MDCoord<2>(arraydims), lin);

    for (i64 i=0; i<20000; ++i) {
        for (i64 j=0; j<20000; ++j) {
            MDCoord<2> c(i,j);
            array->put(c, 1.0);
        }
    }
	delete lin;
    delete array;
}
示例#10
0
void ParentMap::set(uint h, MDArray<double> & v) {
	vector<uint> & v1 = indices[h];
	uint i = v1.back();
	assert(v.get_shape().size() == 1);
	uint dim = v.get_shape().front();
	uint k = 0;
	for (uint j = i; j < i + dim; j++, k++) {
		*(seq[j]) = v[k];
	}
}
示例#11
0
MDArray<double> Shrinkage::combine(MDArray<double> target,MDArray<double> MLE,double lambda)
{
	assert(target.get_shape() == MLE.get_shape());

	//        assert(lambda>=0 && lambda<=1);
	lambda = std::max(lambda, 0.0);
	lambda = std::min(lambda, 1.0);

	return add_matrices(mult_num_to_matrix(target,lambda),mult_num_to_matrix(MLE,1-lambda));
}
示例#12
0
Shrinkage::Shrinkage(MDArray<double> data, MDArray<double> MLE)
{	
        N=data.get_shape()[0];
        P=data.get_shape()[1];
	assert(MLE.get_shape()[0]==P && MLE.get_shape()[1]==P);
        this->data=data;
        wk=wk_StDM=vec(N,P,P);
        S_MLE=vec(P,P);
        StDM=vec(N,P);
	set_S_MLE(MLE);
}
示例#13
0
    void FieldFunction::operator()(MDArray& input_phy_points, MDArray& output_field_values,
                                   const stk_classic::mesh::Bucket& bucket, const MDArray& parametric_coordinates, double time_value_optional)
    {
      EXCEPTWATCH;
#ifndef NDEBUG
      int num_elements_in_bucket   = bucket.size();
      VERIFY_OP(input_phy_points.dimension(0), ==, num_elements_in_bucket, "FieldFunction::operator() mismatch in input_phy_points and num_elements_in_bucket");
      VERIFY_OP(output_field_values.dimension(0), ==, num_elements_in_bucket, "FieldFunction::operator() mismatch in input_phy_points and num_elements_in_bucket");
#endif
      helper(input_phy_points, output_field_values, bucket, parametric_coordinates, time_value_optional);
    }
示例#14
0
void printMDArray(const MDArray<nDim> &array)
{
	MDCoord<nDim> dim = array.getDims();
	Datum_t datum;
    for (int i=0; i<dim[0]; i++) {
        for (int j=0; j<dim[1]; j++) {
            MDCoord<2> c(i,j);
            array.get(c, datum);
			cout<<datum<<"\t";
           }
        cout<<endl;
    }
}
void IntrepidSideCell<MDArray>::mapToCellPhysicalFrame( 
    const MDArray& parametric_coords, MDArray& physical_coords )
{
    DTK_REQUIRE( 2 == parametric_coords.rank() );
    DTK_REQUIRE( 3 == physical_coords.rank() );
    DTK_REQUIRE( parametric_coords.dimension(1) ==
		   Teuchos::as<int>(this->d_topology.getDimension()) );
    DTK_REQUIRE( physical_coords.dimension(0) ==
		   this->d_cell_node_coords.dimension(0) );
    DTK_REQUIRE( physical_coords.dimension(1) ==
		   parametric_coords.dimension(0) );
    DTK_REQUIRE( physical_coords.dimension(2) ==
		   Teuchos::as<int>(d_parent_topology.getDimension()) );

    MDArray mapped_coords( parametric_coords.dimension(0),
			   d_parent_topology.getDimension() );

    Intrepid::CellTools<Scalar>::mapToReferenceSubcell(
	mapped_coords, parametric_coords, this->d_topology.getDimension(),
	d_side_id, d_parent_topology );

    Intrepid::CellTools<Scalar>::mapToPhysicalFrame( 
	physical_coords, mapped_coords, 
	this->d_cell_node_coords, d_parent_topology );
}
示例#16
0
TEST(Gen, GenerateData)
{
    i64 rows = 5L;
    i64 cols = 5L;
    MDCoord dim(2, rows, cols);
    StorageType type = DMA;
    i64 blockDims[] = {3L, 2L};
    u8 blockOrders[] = {1, 0};
    u8 microOrders[] = {0, 1};
    //permute(blockOrders, dim.nDim);
    //permute(microOrders, dim.nDim);
    Linearization *block = new BlockBased(dim.nDim, dim.coords, blockDims,
                                          blockOrders, microOrders);
    //Linearization *block = new BlockBased(dim.nDim, dim.coords, blockDims,
    //                                      blockOrders, microOrders);
    const char *fileName = "b.bin";
    MDCoord key;
    Datum_t datum = 0.0;

    MDArray *array = new MDArray(dim, type, block, fileName);
    
    MDIterator *it = array->createIterator(Dense, block);

    while(it->moveNext()) {
        it->put(datum);
        datum += 1;
    }
    
    delete it;

    delete array;

    array = new MDArray(fileName);
    it = array->createIterator(Dense, block);
    while(it->moveNext()) {
        it->get(key, datum);
        cout<<"key "<<key.toString()<<"\t datum "<<datum<<endl;
    }
    delete it;
    delete array;
    
    delete block;
}
示例#17
0
void checkRect(const MDArray<2> &array, const MDCoord<2> &begin, const MDCoord<2> &end, const Datum_t *data)
{
	int k = 0;
	Datum_t datum;
	for (i64 j=begin[1]; j<=end[1]; ++j) 
		for (i64 i=begin[0]; i<=end[0]; ++i) {
			array.get(MDCoord<2>(i,j), datum);
			ASSERT_EQ(data[k++], datum);
		}
}
示例#18
0
Shrinkage::Shrinkage(MDArray<double> data)
{
        N=data.get_shape()[0];
        P=data.get_shape()[1];
        this->data=data;
        wk=wk_StDM=vec(N,P,P);
        S_MLE=vec(P,P);
        StDM=vec(N,P);
	generate_MLE();
}
示例#19
0
/// random insertion into nxn array, all indices are written to once
void randomInsert()
{
   i64 k[n*n];
   int stride = 5;
   int num = n*n/stride;
   for (int i = 0; i < num; i++)
   {
      k[i] = i*stride;
   }
    MDArray *array;
    Btree::MSplitter<Datum_t> leafSp;
    Btree::MSplitter<PID_t> intSp;
    if (type == DMA)
        array = new MDArray(dim, type, row, fileName);
    else if (type == BTREE)
        array = new MDArray(dim, row, &leafSp, &intSp, fileName);
   PagedStorageContainer::resetPerfCounts();
   permute(k, num);
   i64 *i = k;

   gettimeofday(&tim, NULL);
   t1 = tim.tv_sec + tim.tv_usec/1000000.0;
   while (i < k + num)
   {
      coord = MDCoord(2, *i/n, *i%n);
      array->put(coord, 12345);
      i++;
   }
   delete array;

   gettimeofday(&tim, NULL);
   t2 = tim.tv_sec + tim.tv_usec/1000000.0;
   readCount += PagedStorageContainer::readCount;
   writeCount += PagedStorageContainer::writeCount;
   accessTime += PagedStorageContainer::accessTime;
   execTime += (t2 - t1);

   fprintf(pFile, "sparsity %4.3f\n", 1.0/stride);
   fprintf(pFile, "%-14s %8i %8i %11.3f %11.3f\n", "rand insert", PagedStorageContainer::readCount, PagedStorageContainer::writeCount, PagedStorageContainer::accessTime, t2-t1);


}
示例#20
0
void MultinomialDensities::set_parameters(MDArray<double> & cpd)
{
    // Check cpd is right shape
    vector<uint> shape = cpd.get_shape();
    assert(shape[1] == dim);

    this->cpd = cpd;
    this->cpd.normalize();
    // make new MultinomialDensity objects
    _create_densities();
}
示例#21
0
 static inline void first_dimensions(MDArray& arr, int arr_offset, int *n_points, int max_rank=3)
 {
   for (int ii = 0; ii < max_rank; ii++)
     {
       n_points[ii] = 1;
     }
   for (int ii = 0; ii < arr_offset; ii++)
     {
       n_points[ii] = arr.dimension(ii);
     }
 }
示例#22
0
void Shrinkage::generate_wk(MDArray<double> mat,int SDM_type)
{
        int n=mat.get_shape()[0];
        int p=mat.get_shape()[1];
        vector<uint> index;
        double sum,term1,term2;
        MDArray<double> sub_means(get_sub_means(mat));
        for(int i=0;i<p;i++)
                for(int j=0;j<p;j++){
                        sum=0;
                        for(int k=0;k<n;k++){
                                term1=mat.get(k,i)-sub_means[i];
                                term2=mat.get(k,j)-sub_means[j];
                                index=vec((uint)k, (uint)i, (uint)j);
                                if(SDM_type)
                                        wk_StDM[index]=term1*term2;
                                else
                                        wk[index]=term1*term2;
                        }
        }
}
示例#23
0
void PoissonSolver::solve(MDArray<Real>& a_Rhs) const
{
	//create a contructor
  Box bx = a_Rhs.getBox(); 
  MDArray<complex<double> > fftwForward(bx);
  MDArray<complex<double> > fourierCoef(bx);
  
  double h = 1/m_M;

  for (int k = 0; k < bx.sizeOf();k++)
  {
		fftwForward[k] = complex<double>(a_Rhs[k],0.);
  }
  FFTMD fftmd = FFTMD(m_fft1dptr);
  fftmd.forwardCC(fftwForward);
 
	 for (int i= 0; i<m_M; i++)
	 {
		 for (int j= 0; j<m_M; j++)
		 {
			 int index[2] ={i,j};
			 
			 if(i ==0 && j ==0)
			 {
				 fourierCoef[index] =complex<double>(0,0);
			 }
			 else
			 {
				 complex <double> div(2*cos(2*M_PI*i*h) - 2*cos(2*M_PI*j*h - 4),0);
				 fourierCoef[index] = fftwForward[index]/div;
			 }

		 }
	 }


	 fftmd.inverseCC(fourierCoef);

	 for (int k= 0; k < bx.sizeOf(); k++)
	 {
		
           a_Rhs[k] = real(fourierCoef[k])/m_N;
		 
	 }

}
示例#24
0
  void ForwardBacktracker::backtrack_pass(uint start, uint end, uint seq_len, MDArray<eMISMASK> & mismask, RandomGen* rg) {
	uint choice = 0;
	MDArray<double> transition_matrix(vec(hidden_node_size));

	// The backtrack loop
	for(int l=end-1; l >= ((int) start); l--) {
		uint i = l - start;
		DiscreteNode *node;

		// Setup the node, cpd and nodes
		if(i==0) {
			node = hd_0;
		}
		else {
			node = hd_1;
		}

		// Choose whether to sample from the forward probability
		// distribution or take the previous choice into account
		if ( ((uint) l)==end-1 ) {
			transition_matrix = forward.get_view(i);
		}
		else {
			// TODO: Make this sum faster
			for (uint j=0; j<hidden_node_size; j++)
				transition_matrix[j] = forward.get(i,j) * transition_matrices[i+1]->get(j, choice);
			transition_matrix.normalize();
		}

		// Sample the hidden node from the forward probability distribution
		transition_matrix.cumsum();
		assert(rg);
		double r = rg->get_rand();
		choice = transition_matrix.bisect(r);
		node->parentmap.set(l, choice);

		// Sample the children
		for(vector<Node*>::iterator child=node->children_1.begin(); child < node->children_1.end(); child++) {
			if (mismask.get(l, (*child)->node_index) != MOCAPY_OBSERVED) {
				(*child)->sample(l);
			}
		}
	}
}
示例#25
0
void PoissonSolver::solve(MDArray<Real>& a_Rhs) const {
    //create a contructor
    Box bx = a_Rhs.getBox();
    MDArray<complex<double> > fftwForward(bx);
    MDArray<complex<double> > fourierCoef(bx);

    double h = 1.0/((double)m_N);
    h = .0001;
    //double h = 1/m_N; // integer division, becomes 0.0 in the end!

    for (int k = 0; k < bx.sizeOf(); k++) {
        fftwForward[k] = complex<double>(a_Rhs[k], 0.0);
    }
    FFTMD fftmd = FFTMD(m_fft1dptr);
    fftmd.forwardCC(fftwForward);

    for (int k=0; k<bx.sizeOf(); k++) {
        int index[2];
        bx.tupleIndex(k,index);
        int i = index[0];
        int j = index[1];
        if (k == 0) {
            fourierCoef[index] = complex<double>(0.0,0.0);
        } else {
            complex<double> div((2.0*cos(2.0*M_PI*((double)i)*h) - 2.0*cos(2.0*M_PI*((double)j)*h) - 4.0)/h/h,0.0);
            fourierCoef[index] =fftwForward[index]/div;
        }
    }

    fftmd.inverseCC(fourierCoef);

    for (int k=0; k<bx.sizeOf(); k++) {
        a_Rhs[k] = real(fourierCoef[k]) * h * h;
    }

}
示例#26
0
TEST(DISABLED_MDArray, Create)
{
    i64 rows = 500L;
    i64 cols = 500L;
    MDCoord dim(2, rows, cols);
    StorageType type = DMA;
    Linearization *row = new RowMajor(dim);
    Linearization *col = new ColMajor(dim);
    i64 blockDims[] = {100L, 100L};
    u8 blockOrders[] = {0, 1};
    u8 microOrders[] = {0, 1};
    permute(blockOrders, dim.nDim);
    permute(microOrders, dim.nDim);
    Linearization *block = new BlockBased(dim.nDim, dim.coords, blockDims,
                                          blockOrders, microOrders);
    const char *fileName = "test.bin";

    MDArray *array = new MDArray(dim, type, row, fileName);
    MDCoord coord1 = MDCoord(2, 120, 19);
    ASSERT_TRUE(AC_OK == array->put(coord1, 12345));
    Datum_t datum;
    ASSERT_TRUE(AC_OK == array->get(coord1, datum));
    ASSERT_DOUBLE_EQ(12345, datum);

    MDCoord coord2 = MDCoord(2, 400, 133);
    ASSERT_TRUE(AC_OK == array->put(coord2, 12.345));
    MDCoord coord3 = MDCoord(2, 256, 19);
    ASSERT_TRUE(AC_OK == array->put(coord3, 1000));
    ASSERT_TRUE(AC_OK == array->get(coord2, datum));
    ASSERT_DOUBLE_EQ(12.345, datum);
    ASSERT_TRUE(AC_OK == array->get(coord3, datum));
    ASSERT_DOUBLE_EQ(1000, datum);

    // dense itor, write
    MDIterator *it = array->createIterator(Dense, row);
    MDCoord coord;
    int i = 0;
    while (it->moveNext()) 
    {
        it->put(i);
        i++;
    } 
    ASSERT_EQ(rows*cols, i);
    delete it;

    // block itor, read
    it = array->createIterator(Dense, block);
    i = 0;
    while(it->moveNext())
    {
        it->get(coord, datum);
        ASSERT_DOUBLE_EQ(row->linearize(coord), datum);
        i++;
    }
    ASSERT_EQ(rows*cols, i);
    delete it;

    /*
    // natural itor, read
    it = array->createNaturalIterator(Dense);
    i = 1;
    while(it->moveNext())
    {
        it->get(coord, datum);
        ASSERT_EQ(i, row->linearize(coord));
        ASSERT_EQ(i, datum);
        i++;
    }
    ASSERT_EQ(rows*cols, i);
    delete it;
    */

    delete row;
    delete col;
    delete block;
    delete array;
}
示例#27
0
int main(void) {
   	mocapy_seed((uint)5556575);

	// Number of trainining sequences
	int N = 500;

	// Sequence lengths
	int T = 100;

	// Gibbs sampling parameters
	int MCMC_BURN_IN = 10;

	// HMM hidden and observed node sizes
	uint H_SIZE=2;
	bool init_random=false;

	CPD th0_cpd;
	th0_cpd.set_shape(2); th0_cpd.set_values(vec(0.1, 0.9));

	CPD th1_cpd;
	th1_cpd.set_shape(2,2); th1_cpd.set_values( vec(0.95, 0.05, 0.1, 0.9));

	// The target DBN (This DBN generates the data)
	Node* th0 = NodeFactory::new_discrete_node(H_SIZE, "th0", init_random, th0_cpd);
	Node* th1 = NodeFactory::new_discrete_node(H_SIZE, "th1", init_random, th1_cpd);
	Node* to0 = NodeFactory::new_kent_node("to0");

	DBN tdbn;
	tdbn.set_slices(vec(th0, to0), vec(th1, to0));

	tdbn.add_intra("th0", "to0");
	tdbn.add_inter("th0", "th1");
	tdbn.construct();

	// The model DBN (this DBN will be trained)
	// For mh0, get the CPD from th0 and fix parameters
	Node* mh0 = NodeFactory::new_discrete_node(H_SIZE, "mh0", init_random, CPD(), th0, true );
	Node* mh1 = NodeFactory::new_discrete_node(H_SIZE, "mh1", init_random);
	Node* mo0 = NodeFactory::new_kent_node("mo0");

	DBN mdbn;
	mdbn.set_slices(vec(mh0, mo0), vec(mh1, mo0));

	mdbn.add_intra("mh0", "mo0");
	mdbn.add_inter("mh0", "mh1");
	mdbn.construct();

	cout << "*** TARGET ***" << endl;
	cout << *th0 << endl;
	cout << *th1 << endl;
	cout << *to0 << endl;

	cout << "*** MODEL ***" << endl;
	cout << *mh0 << endl;
	cout << *mh1 << endl;
	cout << *mo0 << endl;

	vector<Sequence> seq_list;
	vector< MDArray<eMISMASK> > mismask_list;

	cout << "Generating data" << endl;

	MDArray<eMISMASK> mismask;
	mismask.repeat(T, vec(MOCAPY_HIDDEN, MOCAPY_OBSERVED));

	// Generate the data
	double sum_LL(0);
	for (int i=0; i<N; i++) {
		pair<Sequence, double>  seq_ll = tdbn.sample_sequence(T);
		sum_LL += seq_ll.second;
 		seq_list.push_back(seq_ll.first);
 		mismask_list.push_back(mismask);
	}
	cout << "Average LL: " << sum_LL/N << endl;

	GibbsRandom mcmc = GibbsRandom(&mdbn);
	EMEngine em = EMEngine(&mdbn, &mcmc, &seq_list, &mismask_list);

	cout << "Starting EM loop" << endl;
	double bestLL=-1000;
	uint it_no_improvement(0);
	uint i(0);
	// Start EM loop
	while (it_no_improvement<10) {
		em.do_E_step(1, MCMC_BURN_IN, true);

		double ll = em.get_loglik();

		cout << "LL= " << ll;

		if (ll > bestLL) {
			cout << " * saving model *" << endl;
			mdbn.save("kent_hmm.dbn");
			bestLL = ll;
			it_no_improvement=0;
		}
		else { it_no_improvement++; cout << endl; }

		i++;
		em.do_M_step();
	}

	cout << "DONE" << endl;

	mdbn.load("kent_hmm.dbn");

	cout << "*** TARGET ***" << endl;
	cout << *th0 << endl;
	cout << *th1 << endl;
	cout << *to0 << endl;

	cout << "*** MODEL ***" << endl;
	cout << *mh0 << endl;
	cout << *mh1 << endl;
	cout << *mo0 << endl;


	delete th0;
	delete th1;
	delete to0;

	delete mh0;
	delete mh1;
	delete mo0;
	return EXIT_SUCCESS;
}
示例#28
0
    void StringFunction::operator()(MDArray& inp, MDArray& out, double time_value_optional)
    {
      EXCEPTWATCH;
      argsAreValid(inp, out);
      PRINT("tmp srk StringFunction::operator() getName()= " << getName() << " inp= " << inp << " out= " << out);

      int domain_rank   = m_domain_dimensions.size();
      int codomain_rank = m_codomain_dimensions.size();

      // FIXME move to argsAreValid
      VERIFY_OP(domain_rank, ==, 1, "StringFunction::operator(): must specify domain Dimensions as rank 1 (for now)");
      VERIFY_OP(codomain_rank, ==, 1, "StringFunction::operator(): must specify codomain Dimensions as rank 1 (for now)");

      int inp_rank      = inp.rank();
      int out_rank      = out.rank();
      int inp_offset    = inp_rank - domain_rank;
      int out_offset    = out_rank - codomain_rank;

      // returns 1,1,1,... etc. if that dimension doesn't exist
      enum { maxRank = 3};
      VERIFY_OP_ON(inp_rank, <=,  maxRank, "StringFunction::operator() input array rank too large");
      VERIFY_OP_ON(out_rank, <=,  maxRank, "StringFunction::operator() output array rank too large");
      int n_inp_points[maxRank] = {1,1,1};
      int n_out_points[maxRank] = {1,1,1};
      first_dimensions(inp, inp_offset, n_inp_points, maxRank);  
      first_dimensions(out, out_offset, n_out_points, maxRank);  

      int nInDim  = m_domain_dimensions[0];
      int nOutDim = m_codomain_dimensions[0];

      MDArray inp_loc(nInDim);
      MDArray out_loc(nOutDim);

      int iDim[maxRank-1] = {0,0};
      double *xyzt[] = {&m_x, &m_y, &m_z, &m_t};

      PRINT("getName()= " << getName() << " n_inp_points= " << n_inp_points[0] << " " << n_inp_points[1] << " " << n_inp_points[2] << " nInDim= " << nInDim);

      m_t = time_value_optional;
      for (iDim[0] = 0; iDim[0] < n_inp_points[0]; iDim[0]++)
        {
          for (iDim[1] = 0; iDim[1] < n_inp_points[1]; iDim[1]++)
            {
              for (int id = 0; id < nInDim; id++)
                {
                  switch (inp_rank)
                    {
                    case 3:
                      inp_loc(id) = inp(iDim[0], iDim[1], id);
                      *xyzt[id] = inp(iDim[0], iDim[1], id);
                      break;
                    case 2:
                      inp_loc(id) = inp(iDim[0], id);
                      *xyzt[id] = inp(iDim[0], id);
                      break;
                    case 1:
                      inp_loc(id) = inp( id);
                      *xyzt[id] = inp( id);
                      break;
                    }
                }

              if (m_func_to_value.size())
                {
                  evalFunctions(inp_loc, time_value_optional);
                }

              //Save the evaluations slightly differently whether it's a scalar or vector valued function
              if ((int)m_v.size() != nOutDim)
                {
                  m_v.resize(nOutDim);
                }

              PRINT("getName()= " << getName() << " about to evaluate m_functionExpr... iDim[0] = " << iDim[0] << " iDim[1]= " << iDim[1]);
              if(nOutDim == 1)
                m_v[0] = m_functionExpr.evaluate();
              else
                m_functionExpr.evaluate();
              PRINT("getName()= " << getName() << " done to evaluate m_functionExpr... iDim[0] = " << iDim[0] << " iDim[1]= " << iDim[1]);

              for (int iOutDim = 0; iOutDim < nOutDim; iOutDim++)
                {
                  switch (out_rank)
                    {
                    case 1: out(iOutDim)                   = m_v[iOutDim]; break;
                    case 2: out(iDim[0], iOutDim)          = m_v[iOutDim]; break;
                    case 3: out(iDim[0], iDim[1], iOutDim) = m_v[iOutDim]; break;
                    default:
                      VERIFY_1("StringFunction::operator() bad output rank");
                    }
                }
            }
        }
      PRINT("getName()= " << getName() << " done in operator()");
    }
void NewtonSolver<NonlinearProblem>::solve( MDArray& u, 
					    NonlinearProblem& problem,
					    const double tolerance,
					    const int max_iters )
{
    DTK_REQUIRE( 2 == u.rank() );

    // Allocate nonlinear residual, Jacobian, Newton update, and work arrays.
    int d0 = u.dimension(0);
    int d1 = u.dimension(1);
    MDArray F( d0, d1 );
    MDArray J( d0, d1, d1 );
    MDArray J_inv( d0, d1, d1 );
    MDArray update( d0, d1 );
    MDArray u_old = u;
    MDArray conv_check( d0 );

    // Compute the initial state.
    NPT::updateState( problem, u );

    // Computen the initial nonlinear residual and scale by -1 to get -F(u).
    NPT::evaluateResidual( problem, u, F );
    Intrepid::RealSpaceTools<Scalar>::scale( 
	F, -Teuchos::ScalarTraits<Scalar>::one() );

    // Compute the initial Jacobian.
    NPT::evaluateJacobian( problem, u, J );

    // Check for degeneracy of the Jacobian. If it is degenerate then the
    // problem is ill conditioned and return very large numbers in the state
    // vector that correspond to no solution.
    MDArray det( 1 );
    Intrepid::RealSpaceTools<Scalar>::det( det, J );
    if ( std::abs(det(0)) < tolerance )
    {
	for ( int m = 0; m < d0; ++m )
	{
	    for ( int n = 0; n < d1; ++n )
	    {
		u(m,n) = std::numeric_limits<Scalar>::max();
	    }
	}
	return;
    }

    // Nonlinear solve.
    for ( int k = 0; k < max_iters; ++k )
    {
	// Solve the linear model, delta_u = J^-1 * -F(u).
	Intrepid::RealSpaceTools<Scalar>::inverse( J_inv, J );
	Intrepid::RealSpaceTools<Scalar>::matvec( update, J_inv, F );

	// Update the solution, u += delta_u.
	Intrepid::RealSpaceTools<Scalar>::add( u, update );

	// Check for convergence.
	Intrepid::RealSpaceTools<Scalar>::subtract( u_old, u );
	Intrepid::RealSpaceTools<Scalar>::vectorNorm( 
	    conv_check, u_old, Intrepid::NORM_TWO );
	if ( tolerance > conv_check(0) )
	{
	    break;
	}

	// Reset for the next iteration.
	u_old = u;

	// Update any state-dependent data from the last iteration using the
	// new solution vector.
	NPT::updateState( problem, u );

	// Compute the nonlinear residual and scale by -1 to get -F(u).
	NPT::evaluateResidual( problem, u, F );
	Intrepid::RealSpaceTools<Scalar>::scale( 
	    F, -Teuchos::ScalarTraits<Scalar>::one() );

	// Compute the Jacobian.
	NPT::evaluateJacobian( problem, u, J );
    }

    // Check for convergence.
    DTK_ENSURE( tolerance > conv_check(0) );
}
示例#30
0
int main(void) {
   	mocapy_seed((uint)924573);

	// Number of trainining sequences
	int N = 2000;

	// Allocate storage in each node.
	int max_num_dat_points = N;

	// Sequence lengths
	int T = 1;

	// Gibbs sampling parameters
	int MCMC_BURN_IN = 10;

	// HMM hidden and observed node sizes
	uint H_SIZE=2;
	uint O_SIZE=20;
	bool init_random=true;

	// The target DBN (This DBN generates the data)
	Node* th0 = NodeFactory::new_discrete_node(H_SIZE, "th0", init_random);
	Node* th1 = NodeFactory::new_discrete_node(H_SIZE, "th1", init_random);

	Node* to0 = NodeFactory::new_GD_node("to0", O_SIZE , max_num_dat_points);
	cout<< "Done making GD node" << endl;

	DBN tdbn;
	tdbn.set_slices(vec(th0, to0), vec(th1, to0));

	tdbn.add_intra("th0", "to0");
	tdbn.add_inter("th0", "th1");


	cout<< "model has been defined" << endl;
	tdbn.construct();
	
	// The model DBN (this DBN will be trained)
	// For mh0, get the CPD from th0 and fix parameters
	Node* mh0 = NodeFactory::new_discrete_node(H_SIZE, "mh0", init_random, CPD(), th0, true );
	Node* mh1 = NodeFactory::new_discrete_node(H_SIZE, "mh1", init_random);
	Node* mo0 = NodeFactory::new_GD_node("mo0",O_SIZE, max_num_dat_points);

	DBN mdbn;
	mdbn.set_slices(vec(mh0, mo0), vec(mh1, mo0));

	mdbn.add_intra("mh0", "mo0");
	mdbn.add_inter("mh0", "mh1");
	mdbn.construct();

	cout << "*** TARGET ***" << endl;
	cout << *th0 << endl;
	cout << *th1 << endl;
	cout << *to0 << endl;

	cout << "*** MODEL ***" << endl;
	cout << *mh0 << endl;
	cout << *mh1 << endl;
	cout << *mo0 << endl;

	vector<Sequence> seq_list;
	vector< MDArray<eMISMASK> > mismask_list;

	cout << "Generating data" << endl;

	MDArray<eMISMASK> mismask;
	mismask.repeat(T, vec(MOCAPY_HIDDEN, MOCAPY_OBSERVED));

	// Setting parameters
	//CPD pars2;
	CPD pars;
	int t = 4;
	pars.set_shape(H_SIZE,2*(O_SIZE-1));
	//pars2.set_shape(H_SIZE,2*(O_SIZE-1));
	vector<double> parvec;
	for(uint i = 0;i < O_SIZE-1;i++){
	  parvec.push_back(t* 1.0);
	}
	for(double i = O_SIZE-1;i >= 1;i--){
	  parvec.push_back(t*i);
	}
	//	pars.set_values(parvec);
	double s = 2;
	for(uint i = 0;i < O_SIZE-1;i++){
	  parvec.push_back(s);
	}
	for(double i = s*(O_SIZE-1);i >= s*1;i=i-s){
	  parvec.push_back(i);
	}
	pars.set_values(parvec);

	((GDNode*)to0)->get_densities()->set_parameters(pars);
	pair<Sequence, double>  seq_ll;	
	// Generate the data
	double sum_LL(0);
	for (int i=0; i<N; i++) {
	  seq_ll = tdbn.sample_sequence(T);
	  sum_LL += seq_ll.second;
	  seq_list.push_back(seq_ll.first);
	  mismask_list.push_back(mismask);
	}
	cout << "Average LL: " << sum_LL/N << endl;

	GibbsRandom mcmc = GibbsRandom(&mdbn);
	EMEngine em = EMEngine(&mdbn, &mcmc, &seq_list, &mismask_list);

	cout << "Starting EM loop" << endl;
	double bestLL=-100000;
	uint it_no_improvement(0);
	uint i(0);
	bool l;
	// Start EM loop

	while (it_no_improvement<100) {
		em.do_E_step(1, MCMC_BURN_IN, true);
		double ll = em.get_loglik();
		cout << "LL= " << ll;

		if (ll > bestLL) {
		  cout << " * saving model *" << endl;
		  cout << *mo0 << endl;	
		  l = true;
		  //mdbn.save("discrete_hmm.dbn");
		  bestLL = ll;
		  it_no_improvement=0;
		}
		else { 
		  it_no_improvement++; cout << endl;
		}
		i++;
		em.do_M_step();
		if (l){
		  l=false;
		}
	}

	cout << "DONE" << endl;

	//	mdbn.load("discrete_hmm.dbn");

	cout << "*** TARGET ***" << endl;
	cout << *th0 << endl;
	cout << *th1 << endl;
	cout << *to0 << endl;

	cout << "*** MODEL ***" << endl;
	cout << *mh0 << endl;
	cout << *mh1 << endl;
	cout << *mo0 << endl;

	delete th0;
	delete th1;
	delete to0;

	delete mh0;
	delete mh1;
	delete mo0;
	return EXIT_SUCCESS;
}