예제 #1
20
void put_check_for_nan(const CppAD::vector<Base>& vec, std::string& file_name)
{
	size_t char_size       = sizeof(Base) * vec.size();
	const char* char_ptr   = reinterpret_cast<const char*>( vec.data() );
# if CPPAD_HAS_MKSTEMP
	char pattern[] = "/tmp/fileXXXXXX";
	int fd = mkstemp(pattern);
	file_name = pattern;
	write(fd, char_ptr, char_size);
	close(fd);
# else
# if CPPAD_HAS_TMPNAM_S
		std::vector<char> name(L_tmpnam_s);
		if( tmpnam_s( name.data(), L_tmpnam_s ) != 0 )
		{	CPPAD_ASSERT_KNOWN(
				false,
				"Cannot create a temporary file name"
			);
		}
		file_name = name.data();
# else
		file_name = tmpnam( CPPAD_NULL );
# endif
	std::fstream file_out(file_name.c_str(), std::ios::out|std::ios::binary );
	file_out.write(char_ptr, char_size);
	file_out.close();
# endif
	return;
}
예제 #2
1
	// The following routine is not yet used or tested.
	void cppad_colpack_symmetric(
		      CppAD::vector<size_t>&         color         ,
		size_t                               n             ,
		const CppAD::vector<unsigned int*>&  adolc_pattern )
	{	size_t i, k;
		CPPAD_ASSERT_UNKNOWN( adolc_pattern.size() == n );
	
		// Use adolc sparsity pattern to create corresponding bipartite graph
		ColPack::GraphColoringInterface graph(
				SRC_MEM_ADOLC,
				adolc_pattern.data(),
				n
		);
	
		// Color the graph with the speciied ordering
		// graph.Coloring("SMALLEST_LAST", "STAR") is slower in adolc testing
		graph.Coloring("SMALLEST_LAST", "ACYCLIC_FOR_INDIRECT_RECOVERY");
	
		// Use coloring information to create seed matrix
		int n_seed_row;
		int n_seed_col;
		double** seed_matrix = graph.GetSeedMatrix(&n_seed_row, &n_seed_col);
		CPPAD_ASSERT_UNKNOWN( size_t(n_seed_col) == n );
	
		// now return coloring in format required by CppAD
		for(i = 0; i < n; i++)
			color[i] = n;
		for(k = 0; k < size_t(n_seed_row); k++)
		{	for(i = 0; i < n; i++)
			{	if( seed_matrix[k][i] != 0.0 ) 
				{	CPPAD_ASSERT_UNKNOWN( color[i] == n );
					color[i] = k;
				}
			}
		}
# ifndef NDEBUG
		for(i = 0; i < n; i++)
			CPPAD_ASSERT_UNKNOWN(color[i] < n || adolc_pattern[i][0] == 0);

		// The coloring above will probably fail this  test.
		// Check that no rows with the same color have overlapping entries:
		CppAD::vector<bool> found(n);
		for(k = 0; k < size_t(n_seed_row); k++)
		{	size_t j, ell;
			for(j = 0; j < n; j++)
				found[j] = false;
			for(i = 0; i < n; i++) if( color[i] == k )
			{	for(ell = 0; ell < adolc_pattern[i][0]; ell++)
				{	j = adolc_pattern[i][1 + ell];
					CPPAD_ASSERT_UNKNOWN( ! found[j] );
					found[j] = true;
				}
			}
		}
# endif
		return;
	}
예제 #3
1
void get_check_for_nan(CppAD::vector<Base>& vec, const std::string& file_name)
{	//
	size_t n = vec.size();
	size_t char_size = sizeof(Base) * n;
	char* char_ptr   = reinterpret_cast<char*>( vec.data() );
	//
	std::fstream file_in(file_name.c_str(), std::ios::in|std::ios::binary );
	file_in.read(char_ptr, char_size);
	//
	return;
}
예제 #4
1
// ----------------------------------------------------------------------
void cppad_colpack_general(
	CppAD::vector<size_t>&               color         ,
	size_t                               m             ,
	size_t                               n             ,
	const CppAD::vector<unsigned int*>&  adolc_pattern )
{	size_t i, k;
	CPPAD_ASSERT_UNKNOWN( adolc_pattern.size() == m );
	CPPAD_ASSERT_UNKNOWN( color.size() == m );

	// Use adolc sparsity pattern to create corresponding bipartite graph
	ColPack::BipartiteGraphPartialColoringInterface graph(
			SRC_MEM_ADOLC,
			adolc_pattern.data(),
			m,
			n
	);

	// row ordered Partial-Distance-Two-Coloring of the bipartite graph 
	graph.PartialDistanceTwoColoring(
		"SMALLEST_LAST", "ROW_PARTIAL_DISTANCE_TWO"
	);

	// Use coloring information to create seed matrix
	int n_seed_row;
	int n_seed_col;
	double** seed_matrix = graph.GetSeedMatrix(&n_seed_row, &n_seed_col);
	CPPAD_ASSERT_UNKNOWN( size_t(n_seed_col) == m );

	// now return coloring in format required by CppAD
	for(i = 0; i < m; i++)
		color[i] = m;
	for(k = 0; k < size_t(n_seed_row); k++)
	{	for(i = 0; i < m; i++)
		{	if( seed_matrix[k][i] != 0.0 ) 
			{	// check that no row appears twice in the coloring
				CPPAD_ASSERT_UNKNOWN( color[i] == m );
				color[i] = k;
			}
		}
	}
# ifndef NDEBUG
	// check that all non-zero rows appear in the coloring
	for(i = 0; i < m; i++)
		CPPAD_ASSERT_UNKNOWN(color[i] < m || adolc_pattern[i][0] == 0);

	// check that no rows with the same color have overlapping entries
	CppAD::vector<bool> found(n);
	for(k = 0; k < size_t(n_seed_row); k++)
	{	size_t j, ell;
		for(j = 0; j < n; j++)
			found[j] = false;
		for(i = 0; i < m; i++) if( color[i] == k )
		{	for(ell = 0; ell < adolc_pattern[i][0]; ell++)
			{	j = adolc_pattern[i][1 + ell];
				CPPAD_ASSERT_UNKNOWN( ! found[j] );
				found[j] = true;
			}
		}
	}
# endif
	return;
}
예제 #5
0
/*!
Determine which rows of a symmetrix sparse matrix can be computed together.

\param color
is a vector with color.size() == m.
For i = 0 , ... , m-1, color[i] is the color for the corresponding row
of the matrix. We say that a sparsity pattern entry (i, j) is valid if
for all i1, such that i1 != i and color[i1]==color[i],
and all j1, such that (i1, j1) is in sparsity pattern, j1 != j.
The coloring is chosen so that for all (i, j) in the sparsity pattern;
either (i, j) or (j, i) is valid (possibly both).

\param m
is the number of rows (and columns) in the matrix.

\param adolc_pattern
is a vector with adolc_pattern.size() == m.
For i = 0 , ... , m-1, and for k = 1, ... ,adolc_pattern[i][0],
the entry with index (i, adolc_pattern[i][k]) is 
in the sparsity pattern for the symmetric matrix.
*/
void cppad_colpack_symmetric(
	CppAD::vector<size_t>&               color         ,
	size_t                               m             ,
	const CppAD::vector<unsigned int*>&  adolc_pattern )
{	size_t i;
	CPPAD_ASSERT_UNKNOWN( adolc_pattern.size() == m );
	CPPAD_ASSERT_UNKNOWN( color.size() == m );

	// Use adolc sparsity pattern to create corresponding bipartite graph
	ColPack::GraphColoringInterface graph(
			SRC_MEM_ADOLC,
			adolc_pattern.data(),
			m
	);

	// Use STAR coloring because it has a direct recovery scheme; i.e.,
	// not necessary to solve equations to extract values.
	graph.Coloring("SMALLEST_LAST", "STAR");

	// Use coloring information to create seed matrix
	int n_seed_row;
	int n_seed_col;
	double** seed_matrix = graph.GetSeedMatrix(&n_seed_row, &n_seed_col);
	CPPAD_ASSERT_UNKNOWN( size_t(n_seed_row) == m );

	// now return coloring for each row in format required by CppAD
	for(i = 0; i < m; i++)
		color[i] = m;
	for(i = 0; i < m; i++)
	{	for(size_t k = 0; k < size_t(n_seed_col); k++)
		{	if( seed_matrix[i][k] != 0.0 ) 
			{	CPPAD_ASSERT_UNKNOWN( color[i] == m );
				color[i] = k;
			}
		}
	}

# ifndef NDEBUG
	// check that every entry in the symetric matrix can be direclty recovered
	size_t i1, i2, j1, j2, k1, k2, nz1, nz2;
	for(i1 = 0; i1 < m; i1++)
	{	nz1 = size_t(adolc_pattern[i1][0]);
		for(k1 = 1; k1 <= nz1; k1++)	
		{	j1 = adolc_pattern[i1][k1];

			// check of a forward on color[i1] followed by a reverse
			// can recover entry (i1, j1)
			bool color_i1_ok = true;
			for(i2 = 0; i2 < m; i2++) if( i1 != i2 && color[i1] == color[i2] )
			{	nz2 = adolc_pattern[i2][0];
				for(k2 = 1; k2 <= nz2; k2++)
				{	j2 = adolc_pattern[i2][k2];	
					color_i1_ok &= (j1 != j2);
				}
			}

			// check of a forward on color[j1] followed by a reverse
			// can recover entry (j1, i1)
			bool color_j1_ok = true;
			for(j2 = 0; j2 < m; j2++) if( j1 != j2 && color[j1] == color[j2] )
			{	nz2 = adolc_pattern[j2][0];
				for(k2 = 1; k2 <= nz2; k2++)
				{	i2 = adolc_pattern[j2][k2];	
					color_j1_ok &= (i1 != i2);
				}
			}

			CPPAD_ASSERT_UNKNOWN( color_i1_ok || color_j1_ok );
		}
	}
# endif
	return;
}
예제 #6
0
// ----------------------------------------------------------------------
CPPAD_LIB_EXPORT void cppad_colpack_general(
    CppAD::vector<size_t>&               color         ,
    size_t                               m             ,
    size_t                               n             ,
    const CppAD::vector<unsigned int*>&  adolc_pattern )
{   size_t i, k;
    CPPAD_ASSERT_UNKNOWN( adolc_pattern.size() == m );
    CPPAD_ASSERT_UNKNOWN( color.size() == m );

    // Use adolc sparsity pattern to create corresponding bipartite graph
    ColPack::BipartiteGraphPartialColoringInterface graph(
            SRC_MEM_ADOLC,
            adolc_pattern.data(),
            m,
            n
    );

    // row ordered Partial-Distance-Two-Coloring of the bipartite graph
    graph.PartialDistanceTwoColoring(
        "SMALLEST_LAST", "ROW_PARTIAL_DISTANCE_TWO"
    );

    // Use coloring information to create seed matrix
    int n_seed_row;
    int n_seed_col;
    double** seed_matrix = graph.GetSeedMatrix(&n_seed_row, &n_seed_col);
    CPPAD_ASSERT_UNKNOWN( size_t(n_seed_col) == m );

    // now return coloring in format required by CppAD
    for(i = 0; i < m; i++)
        color[i] = m;
    for(k = 0; k < size_t(n_seed_row); k++)
    {   for(i = 0; i < m; i++)
        {   if( seed_matrix[k][i] != 0.0 )
            {   // check that entries in the seed matrix are zero or one
                CPPAD_ASSERT_UNKNOWN( seed_matrix[k][i] == 1.0 );
                // check that no row appears twice in the coloring
                CPPAD_ASSERT_UNKNOWN( color[i] == m );
                // only need include rows with non-zero entries
                if( adolc_pattern[i][0] != 0 )
                {   // set color for this row
                    color[i] = k;
                }
            }
        }
    }
# ifndef NDEBUG
    // check non-zero versus color for each row
    for(i = 0; i < m; i++)
    {
        // if there is a color for row i, check that it has non-zero entries
        if(color[i] < m )
            CPPAD_ASSERT_UNKNOWN( adolc_pattern[i][0] != 0 );

        // if there is no color for row i, check that it is empty
        if( color[i] == m )
            CPPAD_ASSERT_UNKNOWN( adolc_pattern[i][0] == 0 );
    }

    // check that no rows with the same color have non-zero entries
    // with the same column index
    CppAD::vector<bool> found(n);
    for(k = 0; k < size_t(n_seed_row); k++)
    {   // k is the color index
        // found: column already has a non-zero entries for this color
        for(size_t j = 0; j < n; j++)
            found[j] = false;
        // for each row with color k
        for(i = 0; i < m; i++) if( color[i] == k )
        {   // for each non-zero entry in this row
            for(size_t ell = 0; ell < adolc_pattern[i][0]; ell++)
            {   // column index for this entry
                size_t j = adolc_pattern[i][1 + ell];
                // check that this is the first non-zero in this column
                CPPAD_ASSERT_UNKNOWN( ! found[j] );
                // found a non-zero in this column
                found[j] = true;
            }
        }
    }
# endif
    return;
}