<commit_before>#=
Copyright (c) 2015, Intel Corporation
All rights reserved.

Redistribution and use in source and binary forms, with or without 
modification, are permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice, 
  this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice, 
  this list of conditions and the following disclaimer in the documentation 
  and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 
THE POSSIBILITY OF SUCH DAMAGE.
=# 


# math functions
libm_math_functions = Set([:sin, :cos, :tan, :asin, :acos, :acosh, :atanh, :log, :log2, :log10, :lgamma, :log1p,:asinh,:atan,:cbrt,:cosh,:erf,:exp,:expm1,:sinh,:sqrt,:tanh, :isnan])
#using Debug

function pattern_match_call_math(fun::TopNode, input::ASCIIString, typ::Type)
    s = ""
    isDouble = typ == Float64 
    isFloat = typ == Float32
    isComplex = typ <: Complex
    isInt = typ <: Integer
    if in(fun.name,libm_math_functions) && (isFloat || isDouble || isComplex)
        @dprintln(3,"FOUND ", fun.name)
        s = string(fun.name)*"("*input*");"
    end

    # abs() needs special handling since fabs() in math.h should be called for floats
    if is(fun.name,:abs) && (isFloat || isDouble || isComplex || isInt)
      @dprintln(3,"FOUND ", fun.name)
      fname = (isInt || isComplex) ? "abs" : (isFloat ? "fabsf" : "fabs")
      s = fname*"("*input*");"
    end
    return s
end

function pattern_match_call_math(fun::TopNode, input::GenSym)
  pattern_match_call_math(fun, from_expr(input), lstate.symboltable[input])
end


function pattern_match_call_math(fun::TopNode, input::SymbolNode)
  pattern_match_call_math(fun, from_expr(input), input.typ)
end

function pattern_match_call_math(fun::GlobalRef, input)
    return pattern_match_call_math(TopNode(fun.name), input)
end

function pattern_match_call_math(fun::ANY, input::ANY)
    return ""
end

function pattern_match_call_throw(fun::GlobalRef, input)
    s = ""
    if fun.name==:throw
        s = "throw(\"Julia throw() called.\")"
    end
    return s
end

function pattern_match_call_throw(fun::ANY, input::ANY)
    return ""
end

function pattern_match_call_powersq(fun::GlobalRef, x::Number, y::Integer)
    s = ""
    if fun.name==:power_by_squaring
        s = "cgen_pown("*from_expr(x)*","*from_expr(y)*")"
    end
    return s
end

function pattern_match_call_powersq(fun::ANY, x::ANY, y::ANY)
    return ""
end

function pattern_match_call_rand(fun::TopNode, RNG::Any, args...)
    res = ""
    if(fun.name==:rand!)
        res = "cgen_distribution(cgen_rand_generator);\n"
    end
    return res 
end

function pattern_match_call_rand(fun::ANY, RNG::ANY, args...)
    return ""
end

function pattern_match_call_randn(fun::TopNode, RNG::Any, IN::Any)
    res = ""
    if(fun.name==:randn!)
        res = "cgen_n_distribution(cgen_rand_generator);\n"
    end
    return res 
end

function pattern_match_call_randn(fun::ANY, RNG::ANY, IN::ANY)
    return ""
end

function pattern_match_call_reshape(fun::GlobalRef, inp::Any, shape::Union{SymbolNode,Symbol,GenSym})
    res = ""
    if(fun.mod == Base && fun.name==:reshape)
        typ = getSymType(shape)
        if istupletyp(typ)
            dim = length(typ.parameters)
            sh = from_expr(shape)
            shapes = mapfoldl(i->sh*".f"*string(i-1), (a,b) -> a*","*b, 1:dim)
            res = from_expr(inp) * ".reshape(" * shapes * ");\n"
        else
            error("call to reshape expects a tuple, but got ", typ)
        end
    end
    return res 
end

function pattern_match_call_reshape(fun::ANY, inp::ANY, shape::ANY)
    return ""
end

function getSymType(a::Union{Symbol,GenSym})
    return lstate.symboltable[a]
end

function getSymType(a::SymbolNode)
    return lstate.symboltable[a.name]
end

function pattern_match_call_gemm(fun::GlobalRef, C::SymAllGen, tA::Char, tB::Char, A::SymAllGen, B::SymAllGen)
    if fun.mod!=Base.LinAlg || fun.name!=:gemm_wrapper!
        return ""
    end
    cblas_fun = ""
    typ = getSymType(A)
    if getSymType(B)!=typ || getSymType(C)!=typ
        return ""
    end
    if typ==Array{Float32,2}
        cblas_fun = "cblas_sgemm"
    elseif typ==Array{Float64,2}
        cblas_fun = "cblas_dgemm"
    else
        return ""
    end
    s = "$(from_expr(C)); "
    m = (tA == 'N') ? from_arraysize(A,1) : from_arraysize(A,2) 
    k = (tA == 'N') ? from_arraysize(A,2) : from_arraysize(A,1) 
    n = (tB == 'N') ? from_arraysize(B,2) : from_arraysize(B,1)

    lda = from_arraysize(A,1)
    ldb = from_arraysize(B,1)
    ldc = m

    CblasNoTrans = 111 
    CblasTrans = 112 
    _tA = tA == 'N' ? CblasNoTrans : CblasTrans
    _tB = tB == 'N' ? CblasNoTrans : CblasTrans
    CblasColMajor = 102


    if mkl_lib!="" || openblas_lib!="" || sys_blas==1
        s *= "$(cblas_fun)((CBLAS_LAYOUT)$(CblasColMajor),(CBLAS_TRANSPOSE)$(_tA),(CBLAS_TRANSPOSE)$(_tB),$m,$n,$k,1.0,
        $(from_expr(A)).data, $lda, $(from_expr(B)).data, $ldb, 0.0, $(from_expr(C)).data, $ldc)"
    else
        println("WARNING: MKL and OpenBLAS not found. Matrix multiplication might be slow. 
        Please install MKL or OpenBLAS and rebuild ParallelAccelerator for better performance.")
        s *= "cgen_$(cblas_fun)($(from_expr(tA!='N')), $(from_expr(tB!='N')), $m,$n,$k, $(from_expr(A)).data, $lda, $(from_expr(B)).data, $ldb, $(from_expr(C)).data, $ldc)"
    end

    return s
end<commit_msg>backward compatibility with CBLAS_ORDER<commit_after>#=
Copyright (c) 2015, Intel Corporation
All rights reserved.

Redistribution and use in source and binary forms, with or without 
modification, are permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice, 
  this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice, 
  this list of conditions and the following disclaimer in the documentation 
  and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 
THE POSSIBILITY OF SUCH DAMAGE.
=# 


# math functions
libm_math_functions = Set([:sin, :cos, :tan, :asin, :acos, :acosh, :atanh, :log, :log2, :log10, :lgamma, :log1p,:asinh,:atan,:cbrt,:cosh,:erf,:exp,:expm1,:sinh,:sqrt,:tanh, :isnan])
#using Debug

function pattern_match_call_math(fun::TopNode, input::ASCIIString, typ::Type)
    s = ""
    isDouble = typ == Float64 
    isFloat = typ == Float32
    isComplex = typ <: Complex
    isInt = typ <: Integer
    if in(fun.name,libm_math_functions) && (isFloat || isDouble || isComplex)
        @dprintln(3,"FOUND ", fun.name)
        s = string(fun.name)*"("*input*");"
    end

    # abs() needs special handling since fabs() in math.h should be called for floats
    if is(fun.name,:abs) && (isFloat || isDouble || isComplex || isInt)
      @dprintln(3,"FOUND ", fun.name)
      fname = (isInt || isComplex) ? "abs" : (isFloat ? "fabsf" : "fabs")
      s = fname*"("*input*");"
    end
    return s
end

function pattern_match_call_math(fun::TopNode, input::GenSym)
  pattern_match_call_math(fun, from_expr(input), lstate.symboltable[input])
end


function pattern_match_call_math(fun::TopNode, input::SymbolNode)
  pattern_match_call_math(fun, from_expr(input), input.typ)
end

function pattern_match_call_math(fun::GlobalRef, input)
    return pattern_match_call_math(TopNode(fun.name), input)
end

function pattern_match_call_math(fun::ANY, input::ANY)
    return ""
end

function pattern_match_call_throw(fun::GlobalRef, input)
    s = ""
    if fun.name==:throw
        s = "throw(\"Julia throw() called.\")"
    end
    return s
end

function pattern_match_call_throw(fun::ANY, input::ANY)
    return ""
end

function pattern_match_call_powersq(fun::GlobalRef, x::Number, y::Integer)
    s = ""
    if fun.name==:power_by_squaring
        s = "cgen_pown("*from_expr(x)*","*from_expr(y)*")"
    end
    return s
end

function pattern_match_call_powersq(fun::ANY, x::ANY, y::ANY)
    return ""
end

function pattern_match_call_rand(fun::TopNode, RNG::Any, args...)
    res = ""
    if(fun.name==:rand!)
        res = "cgen_distribution(cgen_rand_generator);\n"
    end
    return res 
end

function pattern_match_call_rand(fun::ANY, RNG::ANY, args...)
    return ""
end

function pattern_match_call_randn(fun::TopNode, RNG::Any, IN::Any)
    res = ""
    if(fun.name==:randn!)
        res = "cgen_n_distribution(cgen_rand_generator);\n"
    end
    return res 
end

function pattern_match_call_randn(fun::ANY, RNG::ANY, IN::ANY)
    return ""
end

function pattern_match_call_reshape(fun::GlobalRef, inp::Any, shape::Union{SymbolNode,Symbol,GenSym})
    res = ""
    if(fun.mod == Base && fun.name==:reshape)
        typ = getSymType(shape)
        if istupletyp(typ)
            dim = length(typ.parameters)
            sh = from_expr(shape)
            shapes = mapfoldl(i->sh*".f"*string(i-1), (a,b) -> a*","*b, 1:dim)
            res = from_expr(inp) * ".reshape(" * shapes * ");\n"
        else
            error("call to reshape expects a tuple, but got ", typ)
        end
    end
    return res 
end

function pattern_match_call_reshape(fun::ANY, inp::ANY, shape::ANY)
    return ""
end

function getSymType(a::Union{Symbol,GenSym})
    return lstate.symboltable[a]
end

function getSymType(a::SymbolNode)
    return lstate.symboltable[a.name]
end

function pattern_match_call_gemm(fun::GlobalRef, C::SymAllGen, tA::Char, tB::Char, A::SymAllGen, B::SymAllGen)
    if fun.mod!=Base.LinAlg || fun.name!=:gemm_wrapper!
        return ""
    end
    cblas_fun = ""
    typ = getSymType(A)
    if getSymType(B)!=typ || getSymType(C)!=typ
        return ""
    end
    if typ==Array{Float32,2}
        cblas_fun = "cblas_sgemm"
    elseif typ==Array{Float64,2}
        cblas_fun = "cblas_dgemm"
    else
        return ""
    end
    s = "$(from_expr(C)); "
    m = (tA == 'N') ? from_arraysize(A,1) : from_arraysize(A,2) 
    k = (tA == 'N') ? from_arraysize(A,2) : from_arraysize(A,1) 
    n = (tB == 'N') ? from_arraysize(B,2) : from_arraysize(B,1)

    lda = from_arraysize(A,1)
    ldb = from_arraysize(B,1)
    ldc = m

    CblasNoTrans = 111 
    CblasTrans = 112 
    _tA = tA == 'N' ? CblasNoTrans : CblasTrans
    _tB = tB == 'N' ? CblasNoTrans : CblasTrans
    CblasColMajor = 102


    if mkl_lib!="" || openblas_lib!="" || sys_blas==1
        s *= "$(cblas_fun)((CBLAS_ORDER)$(CblasColMajor),(CBLAS_TRANSPOSE)$(_tA),(CBLAS_TRANSPOSE)$(_tB),$m,$n,$k,1.0,
        $(from_expr(A)).data, $lda, $(from_expr(B)).data, $ldb, 0.0, $(from_expr(C)).data, $ldc)"
    else
        println("WARNING: MKL and OpenBLAS not found. Matrix multiplication might be slow. 
        Please install MKL or OpenBLAS and rebuild ParallelAccelerator for better performance.")
        s *= "cgen_$(cblas_fun)($(from_expr(tA!='N')), $(from_expr(tB!='N')), $m,$n,$k, $(from_expr(A)).data, $lda, $(from_expr(B)).data, $ldb, $(from_expr(C)).data, $ldc)"
    end

    return s
end
{'fraction_non_alphanumeric': 0.1340065720926505, 'fraction_numerical': 0.0072132724212551095, 'mean_word_length': 3.8933333333333335, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '4195523', 'n_tokens_mistral': 4534, 'n_tokens_neox': 4303, 'n_words': 1098}