Started on Random Library
I made seed and uniform functions, but they are not omp friendly. They are runnning omp themself, but is not safe for threading/parallizing. Uniform is aproximated and is NOT validaded up against a real uniform distribution.
This commit is contained in:
+20
-13
@@ -99,20 +99,27 @@ template bool print_vector<panic::types::real_t>(const panic::tensor::vector<pan
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
bool print_matrix(const panic::tensor::matrix<T>& A){
|
||||
std::cout << "[";
|
||||
for (panic::types::uint_t i = 0; i < A.rows(); ++i){
|
||||
std::cout << "[";
|
||||
for (panic::types::uint_t j = 0; j < A.cols(); ++j){
|
||||
std::cout << A(i,j) << ", ";
|
||||
}
|
||||
if (i < A.rows()-1){
|
||||
std::cout << A(A.rows()-1,A.cols()-1) << "]" << std::endl;
|
||||
}else{
|
||||
std::cout << A(A.rows()-1,A.cols()-1) << "]]" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
std::cout << "[";
|
||||
for (panic::types::uint_t i = 0; i < A.rows(); ++i){
|
||||
std::cout << "[";
|
||||
|
||||
for (panic::types::uint_t j = 0; j < A.cols(); ++j){
|
||||
std::cout << A(i, j);
|
||||
|
||||
if (j + 1 < A.cols()){
|
||||
std::cout << ", ";
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << "]";
|
||||
|
||||
if (i + 1 < A.rows())
|
||||
std::cout << "," << std::endl;
|
||||
}
|
||||
|
||||
std::cout << "]" << std::endl;
|
||||
return true;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
+2
-2
@@ -51,8 +51,8 @@
|
||||
* Small vectors and matrices are kept serial because the overhead of starting
|
||||
* worker threads can be larger than the work itself.
|
||||
*/
|
||||
static const panic::types::uint_t add_omp_min_work = 10000;
|
||||
|
||||
//static const panic::types::uint_t add_omp_min_work = 10000;
|
||||
static const panic::types::uint_t add_omp_min_work = 0;
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// INPLEMENTATION
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
@@ -0,0 +1,122 @@
|
||||
/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
*
|
||||
* PANIC
|
||||
* Portable Algorithms and Numerics In C++
|
||||
*
|
||||
* Scientific computing from scratch, with feeling.
|
||||
*
|
||||
* Copyright (c) 2026 Michelle Bausager
|
||||
*
|
||||
* This file is part of PANIC.
|
||||
*
|
||||
* PANIC is free software licensed under the GNU General Public License v3.0 or later.
|
||||
* You may redistribute and/or modify it under the terms of the GPL.
|
||||
*
|
||||
* PANIC is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
||||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
* See the LICENSE file for the full license text.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*
|
||||
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
*
|
||||
* Project Name: PANIC
|
||||
* Module Name: random
|
||||
* File Name: seed.cpp
|
||||
* Revision: 0.1.0
|
||||
* Date: 28-06-2026
|
||||
* Author: Michelle Bausager
|
||||
*
|
||||
* Description:
|
||||
* Defines seed for use in other functions in random/
|
||||
*
|
||||
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// INCLUDE DESCRIPTION
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
#include <random/seed.hpp>
|
||||
#include <config/types.hpp>
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// FUNCTION PROTOTYPE
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
namespace panic{
|
||||
namespace random{
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Constructor Name : panic::random::seed::seed_t
|
||||
//
|
||||
// Description:
|
||||
// Creates an seed object.
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
seed_t::seed_t(){
|
||||
value = 1;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Constructor Name : panic::random::seed::seed_t
|
||||
//
|
||||
// Description:
|
||||
// Creates an seed object with a specific seed.
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
seed_t::seed_t(panic::types::uint_t seed){
|
||||
set(seed);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Constructor Name : panic::random::seed::set_seed
|
||||
//
|
||||
// Description:
|
||||
// Sets the seed to a none-zero value
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
bool seed_t::set(panic::types::uint_t seed){
|
||||
|
||||
if (seed == 0){
|
||||
value = 1;
|
||||
}else{
|
||||
value = seed;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Constructor Name : panic::random::seed::get
|
||||
//
|
||||
// Description:
|
||||
// Returns the seed.
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
panic::types::uint_t seed_t::get(){
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Constructor Name : panic::random::seed::state_at
|
||||
//
|
||||
// Description:
|
||||
// Returns a pshodo-random number based on the input index and the seed value.
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
panic::types::uint_t seed_t::state_at(panic::types::uint_t index) const{
|
||||
|
||||
panic::types::uint_t state = value + index;
|
||||
|
||||
// Mix it a few times so close indexes do not start too similarly.
|
||||
//next_state(state);
|
||||
//next_state(state);
|
||||
//next_state(state);
|
||||
|
||||
state ^= state >> 16;
|
||||
state *= static_cast<panic::types::uint_t>(0x7feb352d);
|
||||
state ^= state >> 15;
|
||||
state *= static_cast<panic::types::uint_t>(0x846ca68b);
|
||||
state ^= state >> 16;
|
||||
|
||||
return state;
|
||||
|
||||
}
|
||||
|
||||
|
||||
} // namespace tensor
|
||||
} // namespace panic
|
||||
@@ -0,0 +1,260 @@
|
||||
/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
*
|
||||
* PANIC
|
||||
* Portable Algorithms and Numerics In C++
|
||||
*
|
||||
* Scientific computing from scratch, with feeling.
|
||||
*
|
||||
* Copyright (c) 2026 Michelle Bausager
|
||||
*
|
||||
* This file is part of PANIC.
|
||||
*
|
||||
* PANIC is free software licensed under the GNU General Public License v3.0 or later.
|
||||
* You may redistribute and/or modify it under the terms of the GPL.
|
||||
*
|
||||
* PANIC is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
||||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
* See the LICENSE file for the full license text.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*
|
||||
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
*
|
||||
* Project Name: PANIC
|
||||
* Module Name: random
|
||||
* File Name: uniform.cpp
|
||||
* Revision: 0.1.0
|
||||
* Date: 29-06-2026
|
||||
* Author: Michelle Bausager
|
||||
*
|
||||
* Description:
|
||||
* Defines the dense layers used in neural network
|
||||
*
|
||||
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// INCLUDE DESCRIPTION
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
#include <random/uniform.hpp>
|
||||
#include <config/types.hpp>
|
||||
#include <config/omp.hpp>
|
||||
#include <random/seed.hpp>
|
||||
|
||||
#include <tensor/vector.hpp>
|
||||
|
||||
|
||||
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// VARIABLE DESCRIPTION
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
static const panic::types::uint_t uniform_omp_min_work = 5000;
|
||||
static const panic::types::uint_t max_uint_t = ~static_cast<panic::types::uint_t>(0);
|
||||
static const panic::types::real_t real_max_unit = static_cast<panic::types::real_t>(max_uint_t);
|
||||
|
||||
static panic::types::uint_t uniform_state = static_cast<panic::types::uint_t>(1);
|
||||
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// FUNCTION PROTOTYPE
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
namespace panic{
|
||||
namespace random{
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::random::uniform
|
||||
//
|
||||
// Description:
|
||||
// returns a value from a uniform distribution bewteen 0 and 1
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
panic::types::real_t uniform(){
|
||||
panic::random::seed_t seed;
|
||||
uniform_state += static_cast<panic::types::uint_t>(1);
|
||||
seed.set(uniform_state);
|
||||
|
||||
return (static_cast<panic::types::real_t>(seed.state_at(seed.get()) / real_max_unit));
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::random::uniform
|
||||
//
|
||||
// Description:
|
||||
// returns a value from a uniform distribution bewteen min and max
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
T uniform(const T min, const T max){
|
||||
return (min + static_cast<T>(static_cast<panic::types::real_t>((max - min)) * uniform()));
|
||||
}
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// EXPLICIT TEMPLATE INSTANTIATION
|
||||
//
|
||||
// The implementation is in this .cpp file.
|
||||
// Build the overload for the official PANIC numeric types.
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template panic::types::uint_t
|
||||
uniform<panic::types::uint_t>(const panic::types::uint_t min,
|
||||
const panic::types::uint_t mix
|
||||
);
|
||||
template panic::types::int_t
|
||||
uniform<panic::types::int_t>(const panic::types::int_t min,
|
||||
const panic::types::int_t max
|
||||
);
|
||||
template panic::types::real_t
|
||||
uniform<panic::types::real_t>(const panic::types::real_t min,
|
||||
const panic::types::real_t max
|
||||
);
|
||||
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::random::uniform
|
||||
//
|
||||
// Description:
|
||||
// Fills a vector with a uniform distribution
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
bool uniform(panic::tensor::real_vector& a){
|
||||
|
||||
panic::random::seed_t seed;
|
||||
panic::types::uint_t work = a.size();
|
||||
uniform_state += work;
|
||||
seed.set(uniform_state);
|
||||
|
||||
PANIC_OMP_PARALLEL_FOR_IF(work > uniform_omp_min_work)
|
||||
for (panic::types::uint_t i = 0; i < work; ++i){
|
||||
a[i] = (static_cast<panic::types::real_t>(seed.state_at(i)) / real_max_unit);
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::random::uniform
|
||||
//
|
||||
// Description:
|
||||
// Fills a vector with a uniform distribution with limits
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
bool uniform(panic::tensor::vector<T>& a, const T min, const T max){
|
||||
|
||||
panic::random::seed_t seed;
|
||||
panic::types::uint_t work = a.size();
|
||||
|
||||
uniform_state += work;
|
||||
seed.set(uniform_state);
|
||||
|
||||
panic::types::real_t temp = static_cast<panic::types::real_t>(max - min);
|
||||
|
||||
PANIC_OMP_PARALLEL_FOR_IF(work > uniform_omp_min_work)
|
||||
for (panic::types::uint_t i = 0; i < work; ++i){
|
||||
a[i] = min + static_cast<T>((temp*(static_cast<panic::types::real_t>(seed.state_at(i)) / real_max_unit)));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// EXPLICIT TEMPLATE INSTANTIATION
|
||||
//
|
||||
// The implementation is in this .cpp file.
|
||||
// Build the overload for the official PANIC numeric types.
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template bool uniform<panic::types::uint_t>(panic::tensor::vector<panic::types::uint_t>& a,
|
||||
const panic::types::uint_t min,
|
||||
const panic::types::uint_t mix
|
||||
);
|
||||
template bool uniform<panic::types::int_t>(panic::tensor::vector<panic::types::int_t>& a,
|
||||
const panic::types::int_t min,
|
||||
const panic::types::int_t max
|
||||
);
|
||||
template bool uniform<panic::types::real_t>(panic::tensor::vector<panic::types::real_t>& a,
|
||||
const panic::types::real_t min,
|
||||
const panic::types::real_t max
|
||||
);
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::random::uniform
|
||||
//
|
||||
// Description:
|
||||
// Fills a matrix with a uniform distribution
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
bool uniform(panic::tensor::real_matrix& A){
|
||||
|
||||
panic::random::seed_t seed;
|
||||
|
||||
panic::types::uint_t rows = A.rows();
|
||||
panic::types::uint_t cols = A.cols();
|
||||
|
||||
panic::types::uint_t work = rows*cols;
|
||||
|
||||
uniform_state += work;
|
||||
seed.set(uniform_state);
|
||||
|
||||
PANIC_OMP_PARALLEL_FOR_IF(work > uniform_omp_min_work)
|
||||
for (panic::types::uint_t i = 0; i < rows; ++i){
|
||||
for (panic::types::uint_t j = 0; j < cols; ++j){
|
||||
panic::types::uint_t index = i * cols + j;
|
||||
A(i,j) = (static_cast<panic::types::real_t>(seed.state_at(index)) / real_max_unit);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::random::uniform
|
||||
//
|
||||
// Description:
|
||||
// Fills a matrix with a uniform distribution with limits
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
bool uniform(panic::tensor::matrix<T>& A, const T min, const T max){
|
||||
|
||||
panic::random::seed_t seed;
|
||||
|
||||
panic::types::uint_t rows = A.rows();
|
||||
panic::types::uint_t cols = A.cols();
|
||||
|
||||
panic::types::uint_t work = rows*cols;
|
||||
|
||||
uniform_state += work;
|
||||
seed.set(uniform_state);
|
||||
|
||||
panic::types::real_t temp = static_cast<panic::types::real_t>(max - min);
|
||||
|
||||
PANIC_OMP_PARALLEL_FOR_IF(work > uniform_omp_min_work)
|
||||
for (panic::types::uint_t i = 0; i < rows; ++i){
|
||||
for (panic::types::uint_t j = 0; j < cols; ++j){
|
||||
panic::types::uint_t index = i * cols + j;
|
||||
A(i,j) = min + static_cast<T>((temp*(static_cast<panic::types::real_t>(seed.state_at(index)) / real_max_unit)));
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// EXPLICIT TEMPLATE INSTANTIATION
|
||||
//
|
||||
// The implementation is in this .cpp file.
|
||||
// Build the overload for the official PANIC numeric types.
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template bool uniform<panic::types::uint_t>(panic::tensor::matrix<panic::types::uint_t>& a,
|
||||
const panic::types::uint_t min,
|
||||
const panic::types::uint_t mix
|
||||
);
|
||||
template bool uniform<panic::types::int_t>(panic::tensor::matrix<panic::types::int_t>& a,
|
||||
const panic::types::int_t min,
|
||||
const panic::types::int_t max
|
||||
);
|
||||
template bool uniform<panic::types::real_t>(panic::tensor::matrix<panic::types::real_t>& a,
|
||||
const panic::types::real_t min,
|
||||
const panic::types::real_t max
|
||||
);
|
||||
|
||||
|
||||
} // namespace random
|
||||
} // namespace panic
|
||||
Reference in New Issue
Block a user