text:
//  This matrix class is a C++ wrapper for the GNU Scientific Library
//  Copyright (C)  ULP-IPB Strasbourg

//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2 of the License, or
//  (at your option) any later version.

//  This program 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
//  GNU General Public License for more details.

//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

#ifndef _vector_float_h
#define _vector_float_h

#ifdef __HP_aCC
#include <iostream.h>
#else
#include <iostream>
#endif

#include <gsl/gsl_math.h>
#include <gsl/gsl_vector_float.h>
#include <gsl/gsl_blas.h>
#include <gslwrap/vector_double.h>

//#define NDEBUG 0

#include <assert.h>
namespace gsl
{

#ifndef __HP_aCC
	using std::ostream;
//using std::string;
//using std::runtime_error;
#endif

class vector_float_view;

class vector_float
{
protected:
	gsl_vector_float *gsldata;
	void free(){if(gsldata) gsl_vector_float_free(gsldata);gsldata=NULL;}
	void alloc(size_t n) {gsldata=gsl_vector_float_alloc(n);}
	void calloc(size_t n){gsldata=gsl_vector_float_calloc(n);}
public:
	typedef float value_type;
	vector_float() : gsldata(NULL) {;}
	vector_float( const vector_float &other ):gsldata(NULL) {copy(other);}
	template<class oclass>
	vector_float( const oclass &other ):gsldata(NULL) {copy(other);}
	~vector_float(){free();}
	vector_float(const size_t& n,bool clear=true)
	{
	   this->gsldata = NULL;
		if(clear){this->calloc(n);}
		else     {this->alloc(n);}
	}
	vector_float(const int& n,bool clear=true)
	{
	   this->gsldata = NULL;
		if(clear){this->calloc(n);}
		else     {this->alloc(n);}
	}

	void resize(size_t n);

	template <class oclass>
		void copy(const oclass &other)
		{
			if ( static_cast<const void *>( this ) == static_cast<const void *>( &other ) )
				return;

			if (!other.is_set())
			{
				gsldata=NULL;
				return;
			}
			resize(other.size());
			for (size_t i=0;i<size();i++)
			{
				gsl_vector_float_set(gsldata, i, (float)other[i]);
			}
		}
	void copy(const vector_float& other);
	bool is_set() const{if (gsldata) return true; else return false;}
//	void clone(vector_float& other);

//	size_t size() const {if (!gsldata) {cout << "vector_float::size vector not initialized" << endl; exit(-1);}return gsldata->size;}
	size_t size() const {assert (gsldata); return gsldata->size;}

	/** for interfacing with gsl c */
/*  	gsl_vector_float       *gslobj()       {if (!gsldata){cout << "vector_float::gslobj ERROR, data not initialized!! " << endl; exit(-1);}return gsldata;} */
/*  	const gsl_vector_float *gslobj() const {if (!gsldata){cout << "vector_float::gslobj ERROR, data not initialized!! " << endl; exit(-1);}return gsldata;} */
	gsl_vector_float       *gslobj()       {assert(gsldata);return gsldata;}
	const gsl_vector_float *gslobj() const {assert(gsldata);return gsldata;}

	static vector_float_view create_vector_view( const gsl_vector_float_view &other );

// ********Accessing vector elements

//  Unlike FORTRAN compilers, C compilers do not usually provide support for range checking of vectors and matrices (2). However, the functions gsl_vector_float_get and gsl_vector_float_set can perform range checking for you and report an error if you attempt to access elements outside the allowed range.

//  The functions for accessing the elements of a vector or matrix are defined in `gsl_vector_float.h' and declared extern inline to eliminate function-call overhead. If necessary you can turn off range checking completely without modifying any source files by recompiling your program with the preprocessor definition GSL_RANGE_CHECK_OFF. Provided your compiler supports inline functions the effect of turning off range checking is to replace calls to gsl_vector_float_get(v,i) by v->data[i*v->stride] and and calls to gsl_vector_float_set(v,i,x) by v->data[i*v->stride]=x. Thus there should be no performance penalty for using the range checking functions when range checking is turned off.

//      This function returns the i-th element of a vector v. If i lies outside the allowed range of 0 to n-1 then the error handler is invoked and 0 is returned.
	float get(size_t i) const {return gsl_vector_float_get(gsldata,i);}

//      This function sets the value of the i-th element of a vector v to x. If i lies outside the allowed range of 0 to n-1 then the error handler is invoked.
	void  set(size_t i,float x){gsl_vector_float_set(gsldata,i,x);}

//      These functions return a pointer to the i-th element of a vector v. If i lies outside the allowed range of 0 to n-1 then the error handler is invoked
	float       &operator[](size_t i)       { return *gsl_vector_float_ptr(gsldata,i);}
	const float &operator[](size_t i) const { return *gsl_vector_float_ptr(gsldata,i);}

	float       &operator()(size_t i)       { return *gsl_vector_float_ptr(gsldata,i);}
	const float &operator()(size_t i) const { return *gsl_vector_float_ptr(gsldata,i);}

//  ***** Initializing vector elements

//      This function sets all the elements of the vector v to the value x.
	void set_all(float x){gsl_vector_float_set_all (gsldata,x);}
//      This function sets all the elements of the vector v to zero.
	void set_zero(){gsl_vector_float_set_zero (gsldata);}

//      This function makes a basis vector by setting all the elements of the vector v to zero except for the i-th element which is set to one.
	int set_basis (size_t i) {return gsl_vector_float_set_basis (gsldata,i);}

//  **** Reading and writing vectors

//  The library provides functions for reading and writing vectors to a file as binary data or formatted text.

//      This function writes the elements of the vector v to the stream stream in binary format. The return value is 0 for success and GSL_EFAILED if there was a problem writing to the file. Since the data is written in the native binary format it may not be portable between different architectures.
	int fwrite (FILE * stream) const {return gsl_vector_float_fwrite (stream, gsldata);}

//      This function reads into the vector v from the open stream stream in binary format. The vector v must be preallocated with the correct length since the function uses the size of v to determine how many bytes to read. The return value is 0 for success and GSL_EFAILED if there was a problem reading from the file. The data is assumed to have been written in the native binary format on the same architecture.
	int fread (FILE * stream) {return gsl_vector_float_fread (stream, gsldata);}

	void load( const char *filename );
	///
	void save( const char *filename ) const;

//      This function writes the elements of the vector v line-by-line to the stream stream using the format specifier format, which should be one of the %g, %e or %f formats for floating point numbers and %d for integers. The function returns 0 for success and GSL_EFAILED if there was a problem writing to the file.
	int fprintf (FILE * stream, const char * format) const {return gsl_vector_float_fprintf (stream, gsldata,format) ;}

//      This function reads formatted data from the stream stream into the vector v. The vector v must be preallocated with the correct length since the function uses the size of v to determine how many numbers to read. The function returns 0 for success and GSL_EFAILED if there was a problem reading from the file.
	int fscanf (FILE * stream)  {return gsl_vector_float_fscanf (stream, gsldata); }

//  ******* Vector views

//  In addition to creating vectors from slices of blocks it is also possible to slice vectors and create vector views. For example, a subvector of another vector can be described with a view, or two views can be made which provide access to the even and odd elements of a vector.

//  A vector view is a temporary object, stored on the stack, which can be used to operate on a subset of vector elements. Vector views can be defined for both constant and non-constant vectors, using separate types that preserve constness. A vector view has the type gsl_vector_float_view and a constant vector view has the type gsl_vector_float_const_view. In both cases the elements of the view can be accessed as a gsl_vector_float using the vector component of the view object. A pointer to a vector of type gsl_vector_float * or const gsl_vector_float * can be obtained by taking the address of this component with the & operator.

//      These functions return a vector view of a subvector of another vector v. The start of the new vector is offset by offset elements from the start of the original
//      vector. The new vector has n elements. Mathematically, the i-th element of the new vector v' is given by,

//      v'(i) = v->data[(offset + i)*v->stride]

//      where the index i runs from 0 to n-1.

//      The data pointer of the returned vector struct is set to null if the combined parameters (offset,n) overrun the end of the original vector.

//      The new vector is only a view of the block underlying the original vector, v. The block containing the elements of v is not owned by the new vector. When the
//      new vector goes out of scope the original vector v and its block will continue to exist. The original memory can only be deallocated by freeing the original vector.
//      Of course, the original vector should not be deallocated while the new vector is still in use.

//      The function gsl_vector_float_const_subvector is equivalent to gsl_vector_float_subvector but can be used for vectors which are declared const.
	vector_float_view subvector (size_t offset, size_t n);
	const vector_float_view subvector (size_t offset, size_t n) const;
//	vector_float_const_view subvector (size_t offset, size_t n) const;

//  	class view
//  	{
//  		gsl_vector_float_view *gsldata;
//  	public:
//  		view();
//  	};
//  	view subvector(size_t offset, size_t n)
//  	{
//  		return view(gsl_vector_float_subvector(gsldata,offset,n);
//  	}
//  	const view subvector(size_t offset, size_t n) const
//  	{
//  		return view(gsl_vector_float_const_subvector(gsldata,offset,n);
//  	}

//  Function: gsl_vector_float gsl_vector_float_subvector_with_stride (gsl_vector_float *v, size_t offset, size_t stride, size_t n)
//  Function: gsl_vector_float_const_view gsl_vector_float_const_subvector_with_stride (const gsl_vector_float * v, size_t offset, size_t stride, size_t n)
//      These functions return a vector view of a subvector of another vector v with an additional stride argument. The subvector is formed in the same way as for
//      gsl_vector_float_subvector but the new vector has n elements with a step-size of stride from one element to the next in the original vector. Mathematically,
//      the i-th element of the new vector v' is given by,

//      v'(i) = v->data[(offset + i*stride)*v->stride]

//      where the index i runs from 0 to n-1.

//      Note that subvector views give direct access to the underlying elements of the original vector. For example, the following code will zero the even elements of the
//      vector v of length n, while leaving the odd elements untouched,

//      gsl_vector_float_view v_even = gsl_vector_float_subvector_with_stride (v, 0, 2, n/2);
//      gsl_vector_float_set_zero (&v_even.vector);

//      A vector view can be passed to any subroutine which takes a vector argument just as a directly allocated vector would be, using &view.vector. For example, the
//      following code computes the norm of odd elements of v using the BLAS routine DNRM2,

//      gsl_vector_float_view v_odd = gsl_vector_float_subvector_with_stride (v, 1, 2, n/2);
//      double r = gsl_blas_dnrm2 (&v_odd.vector);

//      The function gsl_vector_float_const_subvector_with_stride is equivalent to gsl_vector_float_subvector_with_stride but can be used for
//      vectors which are declared const.

//  Function: gsl_vector_float_view gsl_vector_float_complex_real (gsl_vector_float_complex *v)
//  Function: gsl_vector_float_const_view gsl_vector_float_complex_const_real (const gsl_vector_float_complex *v)
//      These functions return a vector view of the real parts of the complex vector v.

//      The function gsl_vector_float_complex_const_real is equivalent to gsl_vector_float_complex_real but can be used for vectors which are declared
//      const.

//  Function: gsl_vector_float_view gsl_vector_float_complex_imag (gsl_vector_float_complex *v)
//  Function: gsl_vector_float_const_view gsl_vector_float_complex_const_imag (const gsl_vector_float_complex *v)
//      These functions return a vector view of the imaginary parts of the complex vector v.

//      The function gsl_vector_float_complex_const_imag is equivalent to gsl_vector_float_complex_imag but can be used for vectors which are declared
//      const.

//  Function: gsl_vector_float_view gsl_vector_float_view_array (double *base, size_t n)
//  Function: gsl_vector_float_const_view gsl_vector_float_const_view_array (const double *base, size_t n)
//      These functions return a vector view of an array. The start of the new vector is given by base and has n elements. Mathematically, the i-th element of the new
//      vector v' is given by,

//      v'(i) = base[i]

//      where the index i runs from 0 to n-1.

//      The array containing the elements of v is not owned by the new vector view. When the view goes out of scope the original array will continue to exist. The
//      original memory can only be deallocated by freeing the original pointer base. Of course, the original array should not be deallocated while the view is still in use.

//      The function gsl_vector_float_const_view_array is equivalent to gsl_vector_float_view_array but can be used for vectors which are declared const.

//  Function: gsl_vector_float_view gsl_vector_float_view_array_with_stride (double * base, size_t stride, size_t n)
//  Function: gsl_vector_float_const_view gsl_vector_float_const_view_array_with_stride (const double * base, size_t stride, size_t n)
//      These functions return a vector view of an array base with an additional stride argument. The subvector is formed in the same way as for
//      gsl_vector_float_view_array but the new vector has n elements with a step-size of stride from one element to the next in the original array. Mathematically,
//      the i-th element of the new vector v' is given by,

//      v'(i) = base[i*stride]

//      where the index i runs from 0 to n-1.

//      Note that the view gives direct access to the underlying elements of the original array. A vector view can be passed to any subroutine which takes a vector
//      argument just as a directly allocated vector would be, using &view.vector.

//      The function gsl_vector_float_const_view_array_with_stride is equivalent to gsl_vector_float_view_array_with_stride but can be used for
//      arrays which are declared const.

//  ************* Copying vectors

//  Common operations on vectors such as addition and multiplication are available in the BLAS part of the library (see section BLAS Support). However, it is useful to have a small number of utility functions which do not require the full BLAS code. The following functions fall into this category.

//      This function copies the elements of the vector src into the vector dest.
	vector_float& operator=(const vector_float& other){copy(other);return (*this);}

//  Function: int gsl_vector_float_swap (gsl_vector_float * v, gsl_vector_float * w)
//      This function exchanges the elements of the vectors v and w by copying. The two vectors must have the same length.

//  ***** Exchanging elements

//  The following function can be used to exchange, or permute, the elements of a vector.

//  Function: int gsl_vector_float_swap_elements (gsl_vector_float * v, size_t i, size_t j)
//      This function exchanges the i-th and j-th elements of the vector v in-place.
	int swap_elements (size_t i, size_t j) {return gsl_vector_float_swap_elements (gsldata, i,j);}

//  Function: int gsl_vector_float_reverse (gsl_vector_float * v)
//      This function reverses the order of the elements of the vector v.
	int reverse () {return  gsl_vector_float_reverse (gsldata) ;}

// ******* Vector operations

//  The following operations are only defined for real vectors.

//      This function adds the elements of vector b to the elements of vector a, a'_i = a_i + b_i. The two vectors must have the same length.
	int operator+=(const vector_float &other) {return gsl_vector_float_add (gsldata, other.gsldata);}

//      This function subtracts the elements of vector b from the elements of vector a, a'_i = a_i - b_i. The two vectors must have the same length.
	int operator-=(const vector_float &other) {return gsl_vector_float_sub (gsldata, other.gsldata);}

//  Function: int gsl_vector_float_mul (gsl_vector_float * a, const gsl_vector_float * b)
//      This function multiplies the elements of vector a by the elements of vector b, a'_i = a_i * b_i. The two vectors must have the same length.
	int operator*=(const vector_float &other) {return gsl_vector_float_mul (gsldata, other.gsldata);}

//      This function divides the elements of vector a by the elements of vector b, a'_i = a_i / b_i. The two vectors must have the same length.
	int operator/=(const vector_float &other) {return gsl_vector_float_div (gsldata, other.gsldata);}

//      This function multiplies the elements of vector a by the constant factor x, a'_i = x a_i.
	int operator*=(float x) {return gsl_vector_float_scale (gsldata, x);}

//  Function: int gsl_vector_float_add_constant (gsl_vector_float * a, const double x)
//      This function adds the constant value x to the elements of the vector a, a'_i = a_i + x.
	int operator+=(float x) {return gsl_vector_float_add_constant (gsldata,x);}

//      This function multiplies the elements of vector a by the constant factor x, a'_i = x a_i.
	int operator/=(float x) {return gsl_vector_float_scale (gsldata, 1/x);}

// bool operators:
	bool operator==(const vector_float& other) const;
	bool operator!=(const vector_float& other) const { return (!((*this)==other));}

// stream output:
//	friend ostream& operator<< ( ostream& os, const vector_float& vect );
	/** returns sum of all the vector elements. */
    float sum() const;
	// returns sqrt(v.t*v);
    double norm2() const;

// **** Finding maximum and minimum elements of vectors

//      This function returns the maximum value in the vector v.
    double max() const{return gsl_vector_float_max (gsldata) ;}

//  Function: double gsl_vector_float_min (const gsl_vector_float * v)
//      This function returns the minimum value in the vector v.
    double min() const{return gsl_vector_float_min (gsldata) ;}

//  Function: void gsl_vector_float_minmax (const gsl_vector_float * v, double * min_out, double * max_out)
//      This function returns the minimum and maximum values in the vector v, storing them in min_out and max_out.

//      This function returns the index of the maximum value in the vector v. When there are several equal maximum elements then the lowest index is returned.
	size_t max_index(){return gsl_vector_float_max_index (gsldata);}

//  Function: size_t gsl_vector_float_min_index (const gsl_vector_float * v)
//      This function returns the index of the minimum value in the vector v. When there are several equal minimum elements then the lowest index is returned.
	size_t min_index(){return gsl_vector_float_min_index (gsldata);}

//  Function: void gsl_vector_float_minmax_index (const gsl_vector_float * v, size_t * imin, size_t * imax)
//      This function returns the indices of the minimum and maximum values in the vector v, storing them in imin and imax. When there are several equal minimum
//      or maximum elements then the lowest indices are returned.

//  Vector properties

//  Function: int gsl_vector_float_isnull (const gsl_vector_float * v)
//      This function returns 1 if all the elements of the vector v are zero, and 0 otherwise. };
	bool isnull(){return gsl_vector_float_isnull (gsldata);}
};

// When you add create a view it will stick to its with the view until you call change_view
// ex:
// matrix_float m(5,5);
// vector_float v(5);
// // ...
// m.column(3) = v; //the 3rd column of the matrix m will equal v.
class vector_float_view : public vector_float
{
 public:
	vector_float_view(const vector_float&     other) :vector_float(){init(other);}
	vector_float_view(const vector_float_view& other):vector_float(){init(other);}
	vector_float_view(const gsl_vector_float& gsl_other) : vector_float() {init_with_gsl_vector(gsl_other);}

	void init(const vector_float& other);
	void init_with_gsl_vector(const gsl_vector_float& gsl_other);
	void change_view(const vector_float& other){init(other);}
 private:
};

ostream& operator<< ( ostream& os, const vector_float & vect );

// vector_type<>::type is a template interface to vector_?
// it is usefull for in templated situations for getting the correct vector type
#define tmp_type_is_float
#ifdef tmp_type_is
typedef vector vector_double;
template<class T>
struct vector_type  {typedef vector_double   type;};

template<class T>
struct value_type  {typedef double   type;};

#else
template<> struct vector_type<float> {typedef vector_float type;};
#endif
#undef tmp_type_is_float

}
#endif// _vector_float_h

ext: h

max_stars_repo_name: entn-at/GlottDNN

max_issues_repo_name: entn-at/GlottDNN

max_forks_repo_name: entn-at/GlottDNN


text:
/**
 *  Copyright (C) 2021 FISCO BCOS.
 *  SPDX-License-Identifier: Apache-2.0
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 * @brief tars implementation for Block
 * @file BlockImpl.h
 * @author: ancelmo
 * @date 2021-04-20
 */
#pragma once
#include "BlockHeaderImpl.h"
#include "TransactionImpl.h"
#include "TransactionMetaDataImpl.h"
#include "TransactionReceiptImpl.h"
#include "bcos-tars-protocol/Common.h"
#include "bcos-tars-protocol/tars/Block.h"
#include "interfaces/protocol/Transaction.h"
#include <bcos-framework/interfaces/crypto/CryptoSuite.h>
#include <bcos-framework/interfaces/protocol/Block.h>
#include <bcos-framework/interfaces/protocol/BlockHeader.h>
#include <gsl/span>
#include <memory>

namespace bcostars
{
namespace protocol
{
class BlockImpl : public bcos::protocol::Block, public std::enable_shared_from_this<BlockImpl>
{
public:
    BlockImpl(bcos::protocol::TransactionFactory::Ptr _transactionFactory,
        bcos::protocol::TransactionReceiptFactory::Ptr _receiptFactory)
      : bcos::protocol::Block(_transactionFactory, _receiptFactory),
        m_inner(std::make_shared<bcostars::Block>()),
        x_mutex(std::make_shared<std::mutex>())
    {}

    ~BlockImpl() override{};

    void decode(bcos::bytesConstRef _data, bool _calculateHash, bool _checkSig) override;
    void encode(bcos::bytes& _encodeData) const override;

    int32_t version() const override { return m_inner->blockHeader.data.version; }
    void setVersion(int32_t _version) override { m_inner->blockHeader.data.version = _version; }

    bcos::protocol::BlockType blockType() const override
    {
        return (bcos::protocol::BlockType)m_inner->type;
    }
    // FIXME: this will cause the same blockHeader calculate hash multiple times
    bcos::protocol::BlockHeader::Ptr blockHeader() override;
    bcos::protocol::BlockHeader::ConstPtr blockHeaderConst() const override;

    bcos::protocol::Transaction::ConstPtr transaction(size_t _index) const override;
    bcos::protocol::TransactionReceipt::ConstPtr receipt(size_t _index) const override;

    // get transaction metaData
    bcos::protocol::TransactionMetaData::ConstPtr transactionMetaData(size_t _index) const override;
    void setBlockType(bcos::protocol::BlockType _blockType) override
    {
        m_inner->type = (int32_t)_blockType;
    }

    // set blockHeader
    void setBlockHeader(bcos::protocol::BlockHeader::Ptr _blockHeader) override;

    void setTransaction(size_t _index, bcos::protocol::Transaction::Ptr _transaction) override
    {
        m_inner->transactions[_index] =
            std::dynamic_pointer_cast<bcostars::protocol::TransactionImpl>(_transaction)->inner();
    }
    void appendTransaction(bcos::protocol::Transaction::Ptr _transaction) override
    {
        m_inner->transactions.emplace_back(
            std::dynamic_pointer_cast<bcostars::protocol::TransactionImpl>(_transaction)->inner());
    }

    void setReceipt(size_t _index, bcos::protocol::TransactionReceipt::Ptr _receipt) override;
    void appendReceipt(bcos::protocol::TransactionReceipt::Ptr _receipt) override;

    void appendTransactionMetaData(bcos::protocol::TransactionMetaData::Ptr _txMetaData) override;

    // get transactions size
    size_t transactionsSize() const override { return m_inner->transactions.size(); }
    size_t transactionsMetaDataSize() const override;
    // get receipts size
    size_t receiptsSize() const override { return m_inner->receipts.size(); }

    void setNonceList(bcos::protocol::NonceList const& _nonceList) override;
    void setNonceList(bcos::protocol::NonceList&& _nonceList) override;
    bcos::protocol::NonceList const& nonceList() const override;

    const bcostars::Block& inner() const { return *m_inner; }
    void setInner(const bcostars::Block& inner) { *m_inner = inner; }
    void setInner(bcostars::Block&& inner) { *m_inner = std::move(inner); }

private:
    std::shared_ptr<bcostars::Block> m_inner;
    mutable bcos::protocol::NonceList m_nonceList;
    std::shared_ptr<std::mutex> x_mutex;
};
}  // namespace protocol
}  // namespace bcostars


text:
#include <gsl/gsl_math.h>
#include <gsl/gsl_cblas.h>
#include "cblas.h"
#include "error_cblas_l3.h"

void
cblas_dsymm (const enum CBLAS_ORDER Order, const enum CBLAS_SIDE Side,
             const enum CBLAS_UPLO Uplo, const int M, const int N,
             const double alpha, const double *A, const int lda,
             const double *B, const int ldb, const double beta, double *C,
             const int ldc)
{
#define BASE double
#include "source_symm_r.h"
#undef BASE
}

ext: c

max_stars_repo_licenses: Net-SNMP Xnet

max_issues_repo_licenses: Net-SNMP Xnet

max_forks_repo_licenses: Net-SNMP Xnet


text:
// Framework for using a CBLAS high performance library:

// General functions defined:

// copy(dest, src, len)

// scal(X, len, alpha): X <- alpha * X

// addScal(X, Y, len, alpha): X <- X + alpha * Y

// matrix_multiply(optA, optB, A, B, C, rows_op_A, cols_op_B, cols_op_A): C <- op(A) * op(B)

#ifndef HIGH_PERF_H
#define HIGH_PERF_H

// To use a CBLAS library, e.g OpenBLAS:
#if defined _OPENBLAS
	#define CBLAS
#endif

#include "settings.h" // For 'Number' definition.

#if defined CBLAS

	#pragma message "OpenBLAS is being used."

	#include <cblas.h>

	#define ORDER CblasRowMajor // Do not modify.
	#define CONVERT_CST 111 // Do not modify, used for converting 'Trans' to 'CblasTrans'.

	#define leadingDimension(rows, cols) \
		(ORDER == CblasRowMajor ? cols : rows)

	// float/double implementation:

	#if defined _FLOAT

		#define _copy(dest, src, len) \
			cblas_scopy(len, src, 1, dest, 1)

		#define _scal(X, len, alpha) \
			cblas_sscal(len, alpha, X, 1)

		#define _axpy(X, Y, len, alpha) \
			cblas_saxpy(len, alpha, Y, 1, X, 1)

		#define _gemm(ORDER, optA, optB, rows_op_A, cols_op_B, cols_op_A, alpha, A, lda, B, ldb, beta, C, ldc) \
			cblas_sgemm(ORDER, optA, optB, rows_op_A, cols_op_B, cols_op_A, alpha, A, lda, B, ldb, beta, C, ldc)

	#elif defined _DOUBLE

		#define _copy(dest, src, len) \
			cblas_dcopy(len, src, 1, dest, 1)

		#define _scal(X, len, alpha) \
			cblas_dscal(len, alpha, X, 1)

		#define _axpy(X, Y, len, alpha) \
			cblas_daxpy(len, alpha, Y, 1, X, 1)

		#define _gemm(ORDER, optA, optB, rows_op_A, cols_op_B, cols_op_A, alpha, A, lda, B, ldb, beta, C, ldc) \
			cblas_dgemm(ORDER, optA, optB, rows_op_A, cols_op_B, cols_op_A, alpha, A, lda, B, ldb, beta, C, ldc)
	#endif

	// CBLAS framework:

	#define copy(dest, src, len) \
		copyVector(dest, src, len)
	// Not using CBLAS _copy here, since memcpy seems to be slightly faster...

	#define scal(X, len, alpha) \
		_scal(X, len, alpha)

	#define addScal(X, Y, len, alpha) \
		_axpy(X, Y, len, alpha)

	#define matrix_multiply(optA, optB, A, B, C, rows_op_A, cols_op_B, cols_op_A) \
	{ \
		CBLAS_TRANSPOSE cblas_optA = optA + CONVERT_CST; \
		CBLAS_TRANSPOSE cblas_optB = optB + CONVERT_CST; \
		int lda, ldb, ldc; \
		if (ORDER == CblasRowMajor) \
		{ \
			lda = cblas_optA == CblasNoTrans ? cols_op_A : rows_op_A; \
			ldb = cblas_optB == CblasNoTrans ? cols_op_B : cols_op_A; \
			ldc = cols_op_B; \
		} \
		else \
		{ \
			lda = cblas_optA == CblasNoTrans ? rows_op_A : cols_op_A; \
			ldb = cblas_optB == CblasNoTrans ? cols_op_A : cols_op_B; \
			ldc = rows_op_A; \
		} \
		_gemm(ORDER, cblas_optA, cblas_optB, rows_op_A, cols_op_B, cols_op_A, 1, A, lda, B, ldb, 0, C, ldc); \
	}

#else // Naive implementations:

	#pragma message "No high performance library is being used, expect slow learning."

	#define copy(dest, src, len) \
		copyVector(dest, src, len)

	#define scal(X, len, alpha) \
		naive_scal(X, len, alpha)

	#define addScal(X, Y, len, alpha) \
		naive_addScal(X, Y, len, alpha)

	#define matrix_multiply(optA, optB, A, B, C, rows_op_A, cols_op_B, cols_op_A) \
		naive_matrix_multiply(optA, optB, A, B, C, rows_op_A, cols_op_B, cols_op_A)
#endif

#endif

max_stars_repo_name: Carath/NeuralLib

max_issues_repo_name: Carath/NeuralLib

max_forks_repo_name: Carath/NeuralLib


text:
#pragma once

#include <halley/maths/vector2.h>
#include <halley/maths/rect.h>
#include <memory>
#include <halley/resources/resource.h>
#include <halley/text/halleystring.h>
#include <halley/data_structures/hash_map.h>
#include <gsl/span>
#include "halley/maths/vector4.h"

namespace Halley
{
	class Sprite;
	class Resources;
	class Serializer;
	class Deserializer;
	class ResourceDataStatic;
	class Texture;
	class ResourceLoader;
	class Material;
	class SpriteSheet;

	class SpriteSheetEntry
	{
	public:
		Vector2f pivot;
		Vector2i origPivot;
		Vector2f size;
		Rect4f coords;
		Vector4s trimBorder;
		Vector4s slices;
		int duration = 0;
		bool rotated = false;
		bool sliced = false;

		void serialize(Serializer& s) const;
		void deserialize(Deserializer& s);

#ifdef ENABLE_HOT_RELOAD
		SpriteSheet* parent = nullptr;
		uint32_t idx = 0;
#endif
	};

	class SpriteSheetFrameTag
	{
	public:
		String name;
		int from = 0;
		int to = 0;

		void serialize(Serializer& s) const;
		void deserialize(Deserializer& s);
	};

	class SpriteHotReloader {
	public:
		virtual ~SpriteHotReloader() = default;

#ifdef ENABLE_HOT_RELOAD
		void addSprite(Sprite* sprite, uint32_t idx) const;
		void removeSprite(Sprite* sprite) const;
		void updateSpriteIndex(Sprite* sprite, uint32_t idx) const;
		void clearSpriteRefs();

	protected:
		class SpritePointerHasher {
		public:
			std::size_t operator()(Sprite* ptr) const noexcept;
		};

		mutable std::unordered_map<Sprite*, uint32_t, SpritePointerHasher> spriteRefs;
#endif
	};

	class SpriteSheet final : public Resource, public SpriteHotReloader
	{
	public:
		SpriteSheet();
		~SpriteSheet();

		const std::shared_ptr<const Texture>& getTexture() const;
		const SpriteSheetEntry& getSprite(const String& name) const;
		const SpriteSheetEntry& getSprite(size_t idx) const;
		const SpriteSheetEntry* tryGetSprite(const String& name) const;
		const SpriteSheetEntry& getDummySprite() const;

		const std::vector<SpriteSheetFrameTag>& getFrameTags() const;
		std::vector<String> getSpriteNames() const;
		const HashMap<String, uint32_t>& getSpriteNameMap() const;

		size_t getSpriteCount() const;
		std::optional<size_t> getIndex(const String& name) const;
		bool hasSprite(const String& name) const;

		void loadJson(gsl::span<const gsl::byte> data);

		void addSprite(String name, const SpriteSheetEntry& sprite);
		void setTextureName(String name);

		std::shared_ptr<Material> getMaterial(const String& name) const;
		void setDefaultMaterialName(String materialName);
		const String& getDefaultMaterialName() const;
		void clearMaterialCache() const;

		static std::unique_ptr<SpriteSheet> loadResource(ResourceLoader& loader);
		constexpr static AssetType getAssetType() { return AssetType::SpriteSheet; }
		void reload(Resource&& resource) override;

		void serialize(Serializer& s) const;
		void deserialize(Deserializer& s);

	private:
		constexpr static int version = 1;

		Resources* resources = nullptr;

		std::vector<SpriteSheetEntry> sprites;
		SpriteSheetEntry dummySprite;
		HashMap<String, uint32_t> spriteIdx;
		std::vector<SpriteSheetFrameTag> frameTags;

		String textureName;
		mutable std::shared_ptr<const Texture> texture;

		String defaultMaterialName;
		mutable HashMap<String, std::weak_ptr<Material>> materials;

		void loadTexture(Resources& resources) const;
		void assignIds();
	};

	class SpriteResource final : public Resource, public SpriteHotReloader
	{
	public:
		SpriteResource();
		SpriteResource(const std::shared_ptr<const SpriteSheet>& spriteSheet, size_t idx);
		~SpriteResource();

		const SpriteSheetEntry& getSprite() const;
		size_t getIdx() const;
		std::shared_ptr<const SpriteSheet> getSpriteSheet() const;

		std::shared_ptr<Material> getMaterial(const String& name) const;
		const String& getDefaultMaterialName() const;

		constexpr static AssetType getAssetType() { return AssetType::Sprite; }
		static std::unique_ptr<SpriteResource> loadResource(ResourceLoader& loader);
		void reload(Resource&& resource) override;

		void serialize(Serializer& s) const;
		void deserialize(Deserializer& s);

	private:
		std::weak_ptr<const SpriteSheet> spriteSheet;
		uint64_t idx = -1;
		Resources* resources = nullptr;
	};
}

max_stars_repo_name: JLouhela/halley

max_issues_repo_name: JLouhela/halley

max_forks_repo_name: JLouhela/halley


text:
#ifndef TTT_GS_GAMESTATESTACK_H
#define TTT_GS_GAMESTATESTACK_H

#include "GameState.h"
#include "../Utilities/Utilities.h"

#include <gsl/util>
#include <gsl/assert>

#include <deque>
#include <memory>
#include <optional>

namespace ttt::gs
{
	class GameStateStack final
	{
	public:
		using SizeType = gsl::index;

		template<typename T, typename... Arg>
		void emplace(Arg &&...args) noexcept(std::is_nothrow_constructible_v<T, Arg...>);
		void pop() noexcept;
		void clear() noexcept;

		void update();
		void render() const noexcept;

		void setBackgroundRender(const std::optional<SizeType> &index) noexcept;
		void setBackgroundUpdate(const std::optional<SizeType> &index) noexcept;

		[[nodiscard]] SizeType getSize() const noexcept;
		[[nodiscard]] SizeType getMaxSize() const noexcept;

	private:
		std::deque<std::unique_ptr<GameState>> gameStates;
		std::optional<SizeType> bgUpdateIndex;
		std::optional<SizeType> bgRenderIndex;

		SizeType popIndex{-1};
		SizeType queuedForPop{0};
	};

	template<typename T, typename... Arg>
	void GameStateStack::emplace(Arg &&...args) noexcept(std::is_nothrow_constructible_v<T, Arg...>)
	{
		gameStates.emplace_back(std::make_unique<T>(std::forward<Arg>(args)...));
	}

	inline void GameStateStack::setBackgroundUpdate(const std::optional<SizeType> &index) noexcept
	{
		Expects(!index || *index < util::getSize(gameStates));
		bgUpdateIndex = index;
	}

	inline void GameStateStack::setBackgroundRender(const std::optional<SizeType> &index) noexcept
	{
		Expects(!index || *index < util::getSize(gameStates));
		bgRenderIndex = index;
	}

	inline GameStateStack::SizeType GameStateStack::getSize() const noexcept { return gameStates.size(); }
	inline GameStateStack::SizeType GameStateStack::getMaxSize() const noexcept { return gameStates.max_size(); }
}

#endif

max_stars_repo_name: itsArtem/TicTacToe

max_issues_repo_name: itsArtem/TicTacToe

max_forks_repo_name: itsArtem/TicTacToe


text:
// SPDX-License-Identifier: MIT
// The MIT License (MIT)
//
// Copyright (c) 2014-2018, Institute for Software & Systems Engineering
// Copyright (c) 2018-2019, Johannes Leupolz
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#ifndef PEMC_EXECUTABLE_MODEL_MODEL_EXECUTOR_H_
#define PEMC_EXECUTABLE_MODEL_MODEL_EXECUTOR_H_

#include <vector>
#include <gsl/span>
#include <cstdint>
#include <atomic>
#include <stack>
#include <limits>
#include <functional>

#include "pemc/basic/tsc_index.h"
#include "pemc/basic/configuration.h"
#include "pemc/basic/label.h"
#include "pemc/basic/model_capacity.h"
#include "pemc/basic/raw_memory.h"
#include "pemc/formula/formula.h"
#include "pemc/generic_traverser/i_transitions_calculator.h"
#include "pemc/generic_traverser/i_pre_state_storage_modifier.h"
#include "pemc/generic_traverser/i_post_state_storage_modifier.h"
#include "pemc/executable_model/temporary_state_storage.h"
#include "pemc/executable_model/i_choice_resolver.h"
#include "pemc/executable_model/abstract_model.h"

namespace pemc {

  class ModelExecutor : public ITransitionsCalculator {
  protected:
      std::unique_ptr<IChoiceResolver> choiceResolver;
      std::unique_ptr<AbstractModel> model;

      // Create a storage for different state vectors and a vector of transitions
      // that can be reused for different calculations. This should prevent garbage.
      TemporaryStateStorage temporaryStateStorage;
      std::vector<TraversalTransition> transitions;

      int32_t preStateStorageModifierStateVectorSize = 0;

      void addTransition();
  public:
      ModelExecutor(const Configuration& conf);

      void setModel(std::unique_ptr<AbstractModel> _model);

      void setChoiceResolver(std::unique_ptr<IChoiceResolver> _choiceResolver);

      virtual int32_t getStateVectorSize();

      virtual void setPreStateStorageModifierStateVectorSize(int32_t _preStateStorageModifierStateVectorSize);

      virtual gsl::span<TraversalTransition> calculateInitialTransitions();

      virtual gsl::span<TraversalTransition> calculateTransitionsOfState(gsl::span<gsl::byte> state);

      virtual void* getCustomPayloadOfLastCalculation();

  };

}

#endif  // PEMC_EXECUTABLE_MODEL_MODEL_EXECUTOR_H_

max_stars_repo_name: joleuger/pemc

max_issues_repo_name: joleuger/pemc

max_forks_repo_name: joleuger/pemc