<commit_before>
{-# LANGUAGE ForeignFunctionInterface, GeneralizedNewtypeDeriving #-}


module Numerical.OpenBLAS.FFI  where 

import Foreign.Ptr
import Foreign()
import Foreign.C.Types
import Data.Complex 

-- /*Set the number of threads on runtime.*/
foreign import ccall unsafe "cblas.h openblas_set_num_threads" openblas_set_num_threads_ffi :: CInt -> IO ()

foreign import ccall unsafe "cblas.h goto_set_num_threads" goto_set_num_threads_ffi :: CInt -> IO ()




newtype CBLAS_Index = CBIndex CSize 

newtype CBLAS_OrderT = CBOInt CUChar
    deriving (Eq,Show)
data CBLAS_Order = CBLAS_RowMajor | CBLAS_ColMajor 
    deriving (Eq,Show)

encodeOrder :: CBLAS_Order -> CBLAS_OrderT
encodeOrder CBLAS_RowMajor = CBOInt 101
encodeOrder CBLAS_ColMajor = CBOInt 102 

newtype CBLAS_TransposeT = CBLAS_TransposeT{ unCBLAS_TransposeT :: CUChar } deriving (Eq, Show)

data CBLAS_Tranpose = CBlasNoTranspose | CBlasTranpose | CBlasConjTranspose | CBlasConjNoTranpose 

encodeTranpose :: CBLAS_Tranpose -> CBLAS_TransposeT
encodeTranpose  CBlasNoTranspose = CBLAS_TransposeT 111
encodeTranpose  CBlasTranpose = CBLAS_TransposeT 112
encodeTranpose  CBlasConjTranspose = CBLAS_TransposeT 113
encodeTranpose  CBlasConjNoTranpose = CBLAS_TransposeT 114

newtype CBLAS_UploT = CBlasUPLO CUChar
    deriving (Eq,Show)
data CBLAS_Uplo = CBUpper | CBLower
    deriving (Eq,Show)

encodeUPLO :: CBLAS_Uplo -> CBLAS_UploT    
encodeUPLO CBUpper = CBlasUPLO 121  
encodeUPLO CBLower = CBlasUPLO 122

newtype CBLAS_DiagT = CBLAS_DiagT CUChar 
    deriving (Show,Eq)


data CBlasDiag = CBlasNonUnit   | CBlasUnit 
    deriving (Eq,Show )
    
encodeDiag :: CBlasDiag -> CBLAS_DiagT    
encodeDiag CBlasNonUnit = CBLAS_DiagT 131
encodeDiag CBlasUnit = CBLAS_DiagT 132

newtype CBLAS_SideT = CBLAS_SideT { unCBLAS_SideT :: CUChar } 
    deriving (Eq, Show)
data CBlasSide = CBlasLeft | CBlasRight 
    deriving (Eq,Show)
encodeSide :: CBlasSide -> CBLAS_SideT    
encodeSide CBlasLeft = CBLAS_SideT 141
encodeSide CBlasRight = CBLAS_SideT 142


--typedef enum CBLAS_ORDER     {CblasRowMajor=101, CblasColMajor=102} CBLAS_ORDER;
--typedef enum CBLAS_TRANSPOSE {CblasNoTrans=111, CblasTrans=112, CblasConjTrans=113, CblasConjNoTrans=114} CBLAS_TRANSPOSE;
--typedef enum CBLAS_UPLO      {CblasUpper=121, CblasLower=122} CBLAS_UPLO;
--typedef enum CBLAS_DIAG      {CblasNonUnit=131, CblasUnit=132} CBLAS_DIAG;
--typedef enum CBLAS_SIDE      {CblasLeft=141, CblasRight=142} CBLAS_SIDE;

--dot products
foreign import ccall unsafe "cblas.h cblas_sdsdot" cblas_sdsdot_ffi :: CInt -> CFloat -> Ptr CFloat -> CInt -> Ptr CFloat -> CInt -> IO CFloat 
foreign import ccall unsafe "cblas.h cblas_dsdot" cblas_dsdot_ffi :: CInt -> Ptr CFloat -> CInt -> Ptr CFloat -> CInt -> IO CDouble
foreign import ccall unsafe "cblas.h cblas_sdot" cblas_sdot_ffi :: CInt -> Ptr CFloat -> CInt -> Ptr CFloat -> CInt -> IO CFloat
foreign import ccall unsafe "cblas.h cblas_ddot" cblas_ddot_ffi :: CInt -> Ptr CDouble -> CInt -> Ptr CDouble -> CInt -> IO CDouble
--CFloat  cblas_sdsdot(  CInt n,   CFloat alpha,   CFloat *x,   CInt incx,   CFloat *y,   CInt incy);
--CDouble cblas_dsdot (  CInt n,   CFloat *x,   CInt incx,   CFloat *y,   CInt incy);
--CFloat  cblas_sdot(  CInt n,   CFloat  *x,   CInt incx,   CFloat  *y,   CInt incy);
--CDouble cblas_ddot(  CInt n,   CDouble *x,   CInt incx,   CDouble *y,   CInt incy);

{-
not doing these right now, because requires handling return value as a complex number,
we can only handle pointers to complex numbers right now
-}
--openblas_complex_CFloat  cblas_cdotu(  CInt n,   CFloat  *x,   CInt incx,   CFloat  *y,   CInt incy);
--openblas_complex_CFloat  cblas_cdotc(  CInt n,   CFloat  *x,   CInt incx,   CFloat  *y,   CInt incy);
--openblas_complex_CDouble cblas_zdotu(  CInt n,   CDouble *x,   CInt incx,   CDouble *y,   CInt incy);
--openblas_complex_CDouble cblas_zdotc(  CInt n,   CDouble *x,   CInt incx,   CDouble *y,   CInt incy);




--- not sure what to do for these complex 
--void  cblas_cdotu_sub(  CInt n,   CFloat  *x,   CInt incx,   CFloat  *y,   CInt incy, openblas_complex_CFloat  *ret);
--void  cblas_cdotc_sub(  CInt n,   CFloat  *x,   CInt incx,   CFloat  *y,   CInt incy, openblas_complex_CFloat  *ret);
--void  cblas_zdotu_sub(  CInt n,   CDouble *x,   CInt incx,   CDouble *y,   CInt incy, openblas_complex_CDouble *ret);
--void  cblas_zdotc_sub(  CInt n,   CDouble *x,   CInt incx,   CDouble *y,   CInt incy, openblas_complex_CDouble *ret);

---- absolute value
foreign import ccall unsafe "cblas.h cblas_sasum" cblas_sasum_ffi :: CInt -> Ptr CFloat -> CInt -> IO CFloat
foreign import ccall unsafe "cblas.h cblas_dasum" cblas_dasum_ffi :: 
    CInt -> Ptr CDouble -> CInt -> IO CDouble
foreign import ccall unsafe "cblas.h cblas_scasum" cblas_casum_ffi :: 
    CInt -> Ptr (Complex CFloat)-> CInt -> IO CFloat
foreign import ccall unsafe "cblas.h cblas_dzasum" cblas_zasum_ffi :: 
    CInt -> Ptr (Complex CDouble) -> CInt -> IO CDouble
--CFloat  cblas_sasum (  CInt n,   CFloat  *x,   CInt incx);
--CDouble cblas_dasum (  CInt n,   CDouble *x,   CInt incx);
--CFloat  cblas_scasum(  CInt n,   CFloat  *x,   CInt incx);
--CDouble cblas_dzasum(  CInt n,   CDouble *x,   CInt incx);



--CFloat  cblas_snrm2 (  CInt N,   CFloat  *X,   CInt incX);
--CDouble cblas_dnrm2 (  CInt N,   CDouble *X,   CInt incX);
--CFloat  cblas_scnrm2(  CInt N,   CFloat  *X,   CInt incX);
--CDouble cblas_dznrm2(  CInt N,   CDouble *X,   CInt incX);

--CBLAS_INDEX cblas_isamax(  CInt n,   CFloat  *x,   CInt incx);<commit_msg>adding l2 norm<commit_after>
{-# LANGUAGE ForeignFunctionInterface, GeneralizedNewtypeDeriving #-}


module Numerical.OpenBLAS.FFI  where 

import Foreign.Ptr
import Foreign()
import Foreign.C.Types
import Data.Complex 

-- /*Set the number of threads on runtime.*/
foreign import ccall unsafe "cblas.h openblas_set_num_threads" openblas_set_num_threads_ffi :: CInt -> IO ()

foreign import ccall unsafe "cblas.h goto_set_num_threads" goto_set_num_threads_ffi :: CInt -> IO ()




newtype CBLAS_Index = CBIndex CSize 

newtype CBLAS_OrderT = CBOInt CUChar
    deriving (Eq,Show)
data CBLAS_Order = CBLAS_RowMajor | CBLAS_ColMajor 
    deriving (Eq,Show)

encodeOrder :: CBLAS_Order -> CBLAS_OrderT
encodeOrder CBLAS_RowMajor = CBOInt 101
encodeOrder CBLAS_ColMajor = CBOInt 102 

newtype CBLAS_TransposeT = CBLAS_TransposeT{ unCBLAS_TransposeT :: CUChar } deriving (Eq, Show)

data CBLAS_Tranpose = CBlasNoTranspose | CBlasTranpose | CBlasConjTranspose | CBlasConjNoTranpose 

encodeTranpose :: CBLAS_Tranpose -> CBLAS_TransposeT
encodeTranpose  CBlasNoTranspose = CBLAS_TransposeT 111
encodeTranpose  CBlasTranpose = CBLAS_TransposeT 112
encodeTranpose  CBlasConjTranspose = CBLAS_TransposeT 113
encodeTranpose  CBlasConjNoTranpose = CBLAS_TransposeT 114

newtype CBLAS_UploT = CBlasUPLO CUChar
    deriving (Eq,Show)
data CBLAS_Uplo = CBUpper | CBLower
    deriving (Eq,Show)

encodeUPLO :: CBLAS_Uplo -> CBLAS_UploT    
encodeUPLO CBUpper = CBlasUPLO 121  
encodeUPLO CBLower = CBlasUPLO 122

newtype CBLAS_DiagT = CBLAS_DiagT CUChar 
    deriving (Show,Eq)


data CBlasDiag = CBlasNonUnit   | CBlasUnit 
    deriving (Eq,Show )
    
encodeDiag :: CBlasDiag -> CBLAS_DiagT    
encodeDiag CBlasNonUnit = CBLAS_DiagT 131
encodeDiag CBlasUnit = CBLAS_DiagT 132

newtype CBLAS_SideT = CBLAS_SideT { unCBLAS_SideT :: CUChar } 
    deriving (Eq, Show)
data CBlasSide = CBlasLeft | CBlasRight 
    deriving (Eq,Show)
encodeSide :: CBlasSide -> CBLAS_SideT    
encodeSide CBlasLeft = CBLAS_SideT 141
encodeSide CBlasRight = CBLAS_SideT 142


--typedef enum CBLAS_ORDER     {CblasRowMajor=101, CblasColMajor=102} CBLAS_ORDER;
--typedef enum CBLAS_TRANSPOSE {CblasNoTrans=111, CblasTrans=112, CblasConjTrans=113, CblasConjNoTrans=114} CBLAS_TRANSPOSE;
--typedef enum CBLAS_UPLO      {CblasUpper=121, CblasLower=122} CBLAS_UPLO;
--typedef enum CBLAS_DIAG      {CblasNonUnit=131, CblasUnit=132} CBLAS_DIAG;
--typedef enum CBLAS_SIDE      {CblasLeft=141, CblasRight=142} CBLAS_SIDE;

--dot products
foreign import ccall unsafe "cblas.h cblas_sdsdot" cblas_sdsdot_ffi :: CInt -> CFloat -> Ptr CFloat -> CInt -> Ptr CFloat -> CInt -> IO CFloat 
foreign import ccall unsafe "cblas.h cblas_dsdot" cblas_dsdot_ffi :: CInt -> Ptr CFloat -> CInt -> Ptr CFloat -> CInt -> IO CDouble
foreign import ccall unsafe "cblas.h cblas_sdot" cblas_sdot_ffi :: CInt -> Ptr CFloat -> CInt -> Ptr CFloat -> CInt -> IO CFloat
foreign import ccall unsafe "cblas.h cblas_ddot" cblas_ddot_ffi :: CInt -> Ptr CDouble -> CInt -> Ptr CDouble -> CInt -> IO CDouble
--CFloat  cblas_sdsdot(  CInt n,   CFloat alpha,   CFloat *x,   CInt incx,   CFloat *y,   CInt incy);
--CDouble cblas_dsdot (  CInt n,   CFloat *x,   CInt incx,   CFloat *y,   CInt incy);
--CFloat  cblas_sdot(  CInt n,   CFloat  *x,   CInt incx,   CFloat  *y,   CInt incy);
--CDouble cblas_ddot(  CInt n,   CDouble *x,   CInt incx,   CDouble *y,   CInt incy);

{-
not doing these right now, because requires handling return value as a complex number,
we can only handle pointers to complex numbers right now
-}
--openblas_complex_CFloat  cblas_cdotu(  CInt n,   CFloat  *x,   CInt incx,   CFloat  *y,   CInt incy);
--openblas_complex_CFloat  cblas_cdotc(  CInt n,   CFloat  *x,   CInt incx,   CFloat  *y,   CInt incy);
--openblas_complex_CDouble cblas_zdotu(  CInt n,   CDouble *x,   CInt incx,   CDouble *y,   CInt incy);
--openblas_complex_CDouble cblas_zdotc(  CInt n,   CDouble *x,   CInt incx,   CDouble *y,   CInt incy);




--- not sure what to do for these complex 
--void  cblas_cdotu_sub(  CInt n,   CFloat  *x,   CInt incx,   CFloat  *y,   CInt incy, openblas_complex_CFloat  *ret);
--void  cblas_cdotc_sub(  CInt n,   CFloat  *x,   CInt incx,   CFloat  *y,   CInt incy, openblas_complex_CFloat  *ret);
--void  cblas_zdotu_sub(  CInt n,   CDouble *x,   CInt incx,   CDouble *y,   CInt incy, openblas_complex_CDouble *ret);
--void  cblas_zdotc_sub(  CInt n,   CDouble *x,   CInt incx,   CDouble *y,   CInt incy, openblas_complex_CDouble *ret);

---- absolute value
foreign import ccall unsafe "cblas.h cblas_sasum" cblas_sasum_ffi :: CInt -> Ptr CFloat -> CInt -> IO CFloat
foreign import ccall unsafe "cblas.h cblas_dasum" cblas_dasum_ffi :: 
    CInt -> Ptr CDouble -> CInt -> IO CDouble
foreign import ccall unsafe "cblas.h cblas_scasum" cblas_casum_ffi :: 
    CInt -> Ptr (Complex CFloat)-> CInt -> IO CFloat
foreign import ccall unsafe "cblas.h cblas_dzasum" cblas_zasum_ffi :: 
    CInt -> Ptr (Complex CDouble) -> CInt -> IO CDouble
--CFloat  cblas_sasum (  CInt n,   CFloat  *x,   CInt incx);
--CDouble cblas_dasum (  CInt n,   CDouble *x,   CInt incx);
--CFloat  cblas_scasum(  CInt n,   CFloat  *x,   CInt incx);
--CDouble cblas_dzasum(  CInt n,   CDouble *x,   CInt incx);


foreign import ccall unsafe "cblas.h cblas_snrm2" cblas_snrm2_ffi :: CInt -> Ptr CFloat -> CInt -> IO CFloat
foreign import ccall unsafe "cblas.h cblas_dnrm2" cblas_dnrm2_ffi :: 
    CInt -> Ptr CDouble -> CInt -> IO CDouble
foreign import ccall unsafe "cblas.h cblas_scnrm2" cblas_scnrm2_ffi :: 
    CInt -> Ptr (Complex CFloat)-> CInt -> IO CFloat
foreign import ccall unsafe "cblas.h cblas_dznrm2" cblas_dznrm2_ffi :: 
    CInt -> Ptr (Complex CDouble) -> CInt -> IO CDouble


--CFloat  cblas_snrm2 (  CInt N,   CFloat  *X,   CInt incX);
--CDouble cblas_dnrm2 (  CInt N,   CDouble *X,   CInt incX);
--CFloat  cblas_scnrm2(  CInt N,   CFloat  *X,   CInt incX);
--CDouble cblas_dznrm2(  CInt N,   CDouble *X,   CInt incX);




--CBLAS_INDEX cblas_isamax(  CInt n,   CFloat  *x,   CInt incx);
{'fraction_non_alphanumeric': 0.0924915562483762, 'fraction_numerical': 0.013943015501861956, 'mean_word_length': 3.8623157894736844, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 95, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6412551', 'n_tokens_mistral': 5006, 'n_tokens_neox': 4787, 'n_words': 1223}