Skip to content

Commit

Permalink
Infrastructure for Kolmogorov-Smirnov tests, (tested!) methods for sa…
Browse files Browse the repository at this point in the history
…mpling with replacement, and changes to include guards (#101)

As usual, the scope of this PR exceeded my original plans.

### Incidental, but notable changes

I removed the include-guard pattern
```c++
#ifndef <file identifier>_hh
#define <file identifier>_hh
// ... code ...
#endif
```
and replaced it with ``#pragma once`` everywhere.

### Main changes

I added three functions to ``RandBLAS/util.hh``:
* sample_indices_iid: sample with replacement from an index set
according to a (nonuniform) probability distribution, as specified by
its cumulative distribution function.
* weights_to_cdf: converts a buffer of nonnegative numbers into a
cumulative distribution function.
* sample_indices_iid_uniform: a more efficient version of
sample_indices_iid, specialized to sampling from the uniform
distribution.

I added a file
> test/test_basic_rng/rng_common.hh

This file is where we'll put code that's used to construct our
statistical tests. Conceptually distinct parts of the file:
* a hard-coded statistical table for running two-sided
Kolmogorov-Smirnov tests, plus functions for performing lookups in this
table.
* a section for purely combinatorial helper functions. Right now there's
only one such function:``log_binomial_coefficient``.
* a section for computing some quantities of interest for the
hypergeometric distribution. The function for constructing the PMF is
important since we can use it in a Kolmogorov-Smirnov test for
correctness of ``repeated_fisher_yates`` (our function for sampling
uniformly from an index set without replacement). Note: I haven't
implemented this test yet!
 
I also added
> test/test_basic_rng/test_sample_indices.cc

Right now it only contains tests for sampling _with replacement_. It
should also contain tests for sampling _without replacement_, where an
empirical CDF for the hypergeometric distribution can be constructed
from ``repeated_fisher_yates`` and the true CDF can be computed from
``test_basic_rng/rng_common.hh``.
  • Loading branch information
rileyjmurray authored Jul 16, 2024
1 parent ec4e6f8 commit dd7d158
Show file tree
Hide file tree
Showing 28 changed files with 593 additions and 140 deletions.
18 changes: 12 additions & 6 deletions RandBLAS/base.hh
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@
// POSSIBILITY OF SUCH DAMAGE.
//

#ifndef randblas_base_hh
#define randblas_base_hh
#pragma once

/// @file

Expand All @@ -54,9 +53,18 @@
/// code common across the project
namespace RandBLAS {

/**
* Stores stride information for a matrix represented as a buffer.
* The intended semantics for a buffer "A" and the conceptualized
* matrix "mat(A)" are
*
* mat(A)[i, j] == A[i * inter_row_stride + j * inter_col_stride].
*
* for all (i, j) within the bounds of mat(A).
*/
struct stride_64t {
int64_t inter_row_stride;
int64_t inter_col_stride;
int64_t inter_row_stride; // step down a column
int64_t inter_col_stride; // step along a row
};

inline stride_64t layout_to_strides(blas::Layout layout, int64_t ldim) {
Expand Down Expand Up @@ -212,5 +220,3 @@ std::ostream &operator<<(
}

} // end namespace RandBLAS::base

#endif
5 changes: 1 addition & 4 deletions RandBLAS/config.h.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#ifndef RandBLAS_config_h
#define RandBLAS_config_h
#pragma once

#define RandBLAS_FULL_VERSION "@RandBLAS_FULL_VERSION@"
#define RandBLAS_VERSION_MAJOR @RandBLAS_VERSION_MAJOR@
Expand Down Expand Up @@ -53,5 +52,3 @@
//
// if you are linking to OpenMP.
//

#endif
6 changes: 1 addition & 5 deletions RandBLAS/dense_skops.hh
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//

#ifndef randblas_dense_hh
#define randblas_dense_hh
#pragma once

#include "RandBLAS/base.hh"
#include "RandBLAS/exceptions.hh"
Expand Down Expand Up @@ -640,5 +638,3 @@ RNGState<RNG> fill_dense(
}

} // end namespace RandBLAS

#endif
5 changes: 0 additions & 5 deletions RandBLAS/exceptions.hh
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@

#pragma once

#ifndef RandBLAS_EXCEPTIONS_HH
#define RandBLAS_EXCEPTIONS_HH

#include <exception>
#include <cstdarg>
#include <string>
Expand Down Expand Up @@ -164,5 +161,3 @@ inline void abort_if( bool cond, const char* func, const char* format, ... ) {
#endif

} // namespace RandBLAS::exceptions

#endif
4 changes: 1 addition & 3 deletions RandBLAS/random_gen.hh
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@
// POSSIBILITY OF SUCH DAMAGE.
//

#ifndef random_gen_hh
#define random_gen_hh
#pragma once

/// @file

Expand Down Expand Up @@ -178,4 +177,3 @@ struct uneg11

} // end of namespace r123ext

#endif
7 changes: 1 addition & 6 deletions RandBLAS/skge.hh
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//

#ifndef randblas_skge_hh
#define randblas_skge_hh
#pragma once

#include "RandBLAS/base.hh"
#include "RandBLAS/exceptions.hh"
Expand Down Expand Up @@ -1231,6 +1229,3 @@ inline void sketch_general(
};

} // end namespace RandBLAS


#endif
4 changes: 1 addition & 3 deletions RandBLAS/sksy.hh
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@
// POSSIBILITY OF SUCH DAMAGE.
//

#ifndef randblas_sksy_hh
#define randblas_sksy_hh
#pragma once

#include "RandBLAS/util.hh"
#include "RandBLAS/base.hh"
Expand Down Expand Up @@ -538,4 +537,3 @@ inline void sketch_symmetric(
}

} // end namespace RandBLAS
#endif
5 changes: 1 addition & 4 deletions RandBLAS/skve.hh
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//

#ifndef randblas_skve_hh
#define randblas_skve_hh
#pragma once

#include "RandBLAS/base.hh"
#include "RandBLAS/exceptions.hh"
Expand Down Expand Up @@ -260,4 +258,3 @@ inline void sketch_vector(
}

} // end namespace RandBLAS
#endif
8 changes: 1 addition & 7 deletions RandBLAS/sparse_data/base.hh
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//

#ifndef randblas_sparse_data_hh
#define randblas_sparse_data_hh
#pragma once

#include "RandBLAS/config.h"
#include "RandBLAS/base.hh"
Expand Down Expand Up @@ -193,7 +191,3 @@ namespace RandBLAS {
using RandBLAS::sparse_data::IndexBase;
using RandBLAS::sparse_data::SparseMatrix;
}



#endif
5 changes: 1 addition & 4 deletions RandBLAS/sparse_data/conversions.hh
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
#pragma once

#ifndef randblas_sparse_data_conversions
#define randblas_sparse_data_conversions
#include "RandBLAS/base.hh"
#include "RandBLAS/exceptions.hh"
#include "RandBLAS/sparse_data/base.hh"
Expand Down Expand Up @@ -209,5 +208,3 @@ void reindex_inplace(COOMatrix<T> &A, IndexBase desired) {
}

} // end namespace RandBLAS::sparse_data::conversions

#endif
5 changes: 2 additions & 3 deletions RandBLAS/sparse_data/coo_matrix.hh
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
// POSSIBILITY OF SUCH DAMAGE.
//

#ifndef randblas_sparse_data_coo
#define randblas_sparse_data_coo
#pragma once

#include "RandBLAS/base.hh"
#include "RandBLAS/exceptions.hh"
#include "RandBLAS/sparse_data/base.hh"
Expand Down Expand Up @@ -409,4 +409,3 @@ void coo_to_dense(const COOMatrix<T> &spmat, Layout layout, T *mat) {

} // end namespace RandBLAS::sparse_data::coo

#endif
6 changes: 2 additions & 4 deletions RandBLAS/sparse_data/coo_spmm_impl.hh
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
// POSSIBILITY OF SUCH DAMAGE.
//

#ifndef randblas_sparse_data_coo_multiply
#define randblas_sparse_data_coo_multiply
#pragma once

#include "RandBLAS/base.hh"
#include "RandBLAS/exceptions.hh"
#include "RandBLAS/sparse_data/base.hh"
Expand Down Expand Up @@ -163,5 +163,3 @@ static void apply_coo_left_jki_p11(


} // end namespace

#endif
9 changes: 3 additions & 6 deletions RandBLAS/sparse_data/csc_matrix.hh
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
// POSSIBILITY OF SUCH DAMAGE.
//

#ifndef randblas_sparse_data_csc
#define randblas_sparse_data_csc
#pragma once

#include "RandBLAS/base.hh"
#include "RandBLAS/exceptions.hh"
#include "RandBLAS/sparse_data/base.hh"
Expand Down Expand Up @@ -246,7 +246,4 @@ void dense_to_csc(Layout layout, T* mat, T abs_tol, CSCMatrix<T> &spmat) {
return;
}


}

#endif
} // end namespace RandBLAS::sparse_data::csc
6 changes: 2 additions & 4 deletions RandBLAS/sparse_data/csc_spmm_impl.hh
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@
// POSSIBILITY OF SUCH DAMAGE.
//

#ifndef randblas_sparse_data_csc_multiply
#define randblas_sparse_data_csc_multiply
#pragma once
#include "RandBLAS/base.hh"
#include "RandBLAS/exceptions.hh"
#include "RandBLAS/sparse_data/base.hh"
Expand Down Expand Up @@ -213,5 +212,4 @@ static void apply_csc_left_kib_rowmajor_1p1(
return;
}

}
#endif
} // end namespace RandBLAS::sparse_data::csc
5 changes: 2 additions & 3 deletions RandBLAS/sparse_data/csr_matrix.hh
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
// POSSIBILITY OF SUCH DAMAGE.
//

#ifndef randblas_sparse_data_csr
#define randblas_sparse_data_csr
#pragma once

#include "RandBLAS/base.hh"
#include "RandBLAS/exceptions.hh"
#include "RandBLAS/sparse_data/base.hh"
Expand Down Expand Up @@ -262,4 +262,3 @@ void dense_to_csr(Layout layout, T* mat, T abs_tol, CSRMatrix<T> &spmat) {


} // end namespace RandBLAS::sparse_data::csr
#endif
6 changes: 2 additions & 4 deletions RandBLAS/sparse_data/csr_spmm_impl.hh
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
// POSSIBILITY OF SUCH DAMAGE.
//

#ifndef randblas_sparse_data_csr_multiply
#define randblas_sparse_data_csr_multiply
#pragma once

#include "RandBLAS/base.hh"
#include "RandBLAS/exceptions.hh"
#include "RandBLAS/sparse_data/base.hh"
Expand Down Expand Up @@ -156,5 +156,3 @@ static void apply_csr_left_ikb_rowmajor(
}

} // end namespace RandBLAS::sparse_data::csr

#endif
6 changes: 1 addition & 5 deletions RandBLAS/sparse_data/sksp.hh
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@
// POSSIBILITY OF SUCH DAMAGE.
//

#ifndef randblas_sksp_hh
#define randblas_sksp_hh
#pragma once

#include "RandBLAS/base.hh"
#include "RandBLAS/dense_skops.hh"
Expand Down Expand Up @@ -625,6 +624,3 @@ inline void sketch_sparse(
}

} // end namespace RandBLAS


#endif
6 changes: 2 additions & 4 deletions RandBLAS/sparse_data/spmm_dispatch.hh
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
// POSSIBILITY OF SUCH DAMAGE.
//

#ifndef randblas_sparse_data_spmm_dispatch
#define randblas_sparse_data_spmm_dispatch
#pragma once

#include "RandBLAS/base.hh"
#include "RandBLAS/exceptions.hh"
#include "RandBLAS/sparse_data/base.hh"
Expand Down Expand Up @@ -384,5 +384,3 @@ inline void spmm(blas::Layout layout, blas::Op opA, blas::Op opB, int64_t m, int
}

}

#endif
6 changes: 1 addition & 5 deletions RandBLAS/sparse_skops.hh
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//

#ifndef randblas_sparse_skops_hh
#define randblas_sparse_skops_hh
#pragma once

#include "RandBLAS/config.h"
#include "RandBLAS/base.hh"
Expand Down Expand Up @@ -483,5 +481,3 @@ static auto transpose(SKOP const &S) {
}

} // end namespace RandBLAS::sparse

#endif
Loading

0 comments on commit dd7d158

Please sign in to comment.