commit cad98f821190330f0d58c2e76f7338b7564298c8 Author: Aaron Date: Sun Jun 22 16:48:23 2025 -0400 init diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..75d2923 --- /dev/null +++ b/LICENSE @@ -0,0 +1,11 @@ +Copyright (c) 2023 Aaron M. Schinder + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + +2. 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. + +3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..eede82e --- /dev/null +++ b/README.md @@ -0,0 +1,9 @@ +# amscppperm1 + +Numeric array template class library. A numeric array, in addition to the usual array operations, is expected to have numeric data that can be added, subtracted, multiplied, etc. + +This numeric array class implements random number generation, threaded vector operations, threaded math and statistics for fast operations on the arrays. + +The primary object is the template class narray + + ams::narray::narray \ No newline at end of file diff --git a/amsnarray.code-workspace b/amsnarray.code-workspace new file mode 100644 index 0000000..8f4183e --- /dev/null +++ b/amsnarray.code-workspace @@ -0,0 +1,7 @@ +{ + "folders": [ + { + "path": "." + } + ] +} diff --git a/backup.sh b/backup.sh new file mode 100644 index 0000000..2bfb732 --- /dev/null +++ b/backup.sh @@ -0,0 +1,5 @@ +#!/usr/bin/bash + +tar -czvf ../amscppnarray.tar.gz ./* + +scp ../amscppnarray.tar.gz aschinder@amssolarempire.com:~/workspace/projects diff --git a/build/__pycache__/amsbuildlib4.cpython-310.pyc b/build/__pycache__/amsbuildlib4.cpython-310.pyc new file mode 100644 index 0000000..c12ef93 Binary files /dev/null and b/build/__pycache__/amsbuildlib4.cpython-310.pyc differ diff --git a/build/__pycache__/amsbuildlib4.cpython-312.pyc b/build/__pycache__/amsbuildlib4.cpython-312.pyc new file mode 100644 index 0000000..5596d1b Binary files /dev/null and b/build/__pycache__/amsbuildlib4.cpython-312.pyc differ diff --git a/build/__pycache__/amsbuildlib4.cpython-39.pyc b/build/__pycache__/amsbuildlib4.cpython-39.pyc new file mode 100644 index 0000000..04d72b0 Binary files /dev/null and b/build/__pycache__/amsbuildlib4.cpython-39.pyc differ diff --git a/build/amsbuildlib4.py b/build/amsbuildlib4.py new file mode 100644 index 0000000..6275726 --- /dev/null +++ b/build/amsbuildlib4.py @@ -0,0 +1,813 @@ +#!/usr/bin/python3 + +import os,sys,math +import subprocess + +""" +Copyright Aaron M. Schinder, 2011 - MIT/BSD License + +This script contains a bunch of helper functions for generating simple, imperative, hopefully +transparent build scripts using the python language (and nothing else). + +I just want the script to do the compiling and linking operations I want it to do in the order +I want it to do it in, finding every relevant source file. + +That's it. That's what I want in a build system. +""" + +def flist(pth,**kwargs): + """ + flist - list all files in a given directory pth + optional arguments: + recurse - (T/F): Whether to recursively search for files in directory tree + exts - (list): A list of file extensions to search for, otherwise all files + normpath (T/F): whether to normalize path variables after + filelist = flist(pth,**kwargs): + """ + flst = [] + if(not('recurse' in kwargs)): + recurse_ = False + else: + recurse_ = kwargs['recurse'] + if(not('exts' in kwargs)): + filterexts_ = False + else: + filterexts_ = True + exts = kwargs['exts'] + if(not('normpath' in kwargs)): + normpath_ = True + else: + normpath_ = kwargs['normpath'] + if(not('linuxpath' in kwargs)): + linuxpath_ = False + else: + linuxpath_ = kwargs['linuxpath'] + if(not('followlinks' in kwargs)): + followlinks_ = False + else: + followlinks_ = kwargs['followlinks'] + + dirlist = [] + rawlist = os.listdir(pth) + + for F in rawlist: + F2 = os.path.join(pth,F) + if(os.path.isdir(F2)): + b = (followlinks_) or ((not followlinks_) and not(os.path.islink(F2))) + if(b): + if((F2!=".")&(F2!="..")): + dirlist.append(F2) + elif(os.path.isfile(F2)): + flst.append(F2) + + #Recurse through directories + if(recurse_): + for D in dirlist: + lst = flist(D,**kwargs) + for L in lst: + flst.append(L) + + #Postprocess: + #Filter out all extensions except the selected ext list + if(filterexts_): + flst = filterexts(flst,exts) + + #Normalize filename path according to os + if(normpath_): + flst2 = list(flst) + for I in range(0,len(flst2)): + flst[I] = os.path.normpath(flst2[I]) + + #If linuxpath, convert all \\ to / + #if(linuxpath_): + # flst2 = list(flst) + # for I in range(0,len(flst2)): + # flst[I] = linuxpath(flst2[I]) + + return flst + + +def filterexts(flst,exts): + """ + Filters by extensions in a list of files + flst = def filterexts(flst,exts): + """ + flst2 = [] + if(isinstance(exts,str)): + exts = list([exts]) + for F in flst: + b = False + for ext in exts: + if(ext[0]!='.'): + ext = '.'+ext + F2 = os.path.splitext(F) + if(len(F2)>=2): + ex = F2[1] + if(len(ex)>0): + if(ex[0]!='.'): + ex = '.'+ex + if(ex==ext): + b = True + if(b): + flst2.append(F) + + return flst2 + +#Find a file fname, starting in pth and recursing +#Used for finding library files to link +def findfile(fname,pth,**kwargs): + fullfname = "" + flst = flist(pth,recurse=True) + for F in flst: + F2 = os.path.split(F)[1] + if(F2 == fname): + fullfname = F + + return fullfname + +def replaceext(fname,ext): + fname2 = "" + if(len(ext)>0): + if(ext[0]!='.'): + ext = '.'+ext + fname2 = os.path.splitext(fname)[0]+ext + else: + fname2 = os.path.splitext(fname)[0] + return fname2 + +def replaceexts(fnamelist,ext): + """Takes a list of filenames and returns a list with the extensions replaced by ext """ + fname2list = [] + for F in fnamelist: + F2 = replaceext(F,ext) + fname2list.append(F2) + return fname2list + +def except_contains(lst1,exc): + """ + Takes a list of file names lst1, and removes filenams that match the + list of exceptions exc. Returns a list without the exceptions. + """ + lst2 = [] + for item in lst1: + b = 1 + for item2 in exc: + fsplit = os.path.split(item) + fn = fsplit[len(fsplit)-1] + if(fn==item2): + b = 0 + break + if(b==1): + lst2.append(item) + return lst2 + + +def list_to_sss(lst): + """List of strings to space-seperated-string""" + lout = "" + for I in range(0,len(lst)-1): + lout = lout + lst[I] + " " + if(len(lst)>0): + lout = lout + lst[len(lst)-1] + return lout + +########################## +##System Call Procedures## +########################## + +def callproc(cmd, **kwargs): + if(not('logfile' in kwargs)): + use_lf = False + else: + logfile = kwargs['logfile'] + if(logfile!=""): + fp = open(kwargs['logfile'],'a+') + use_lf = True + else: + use_lf = False + + if(not('echo' in kwargs)): + echo = True + else: + echo = kwargs['echo'] + + if(echo): + print(cmd) + + #encoding/deconding to/from bytes is necessary to use the subprocess command + #in python3.7 + #However, only do this in linux + if(sys.platform!='win32'): + cmd2 = cmd.encode(encoding='utf-8') + else: + cmd2 = cmd + proc = subprocess.Popen(cmd2,stderr = subprocess.STDOUT, stdout=subprocess.PIPE, shell=True) + (out, err) = proc.communicate() + + out = out.decode(encoding='utf-8') + + if(echo): + print(out) + #print(err); + if(use_lf): + fp.writelines(cmd+'\n') + fp.writelines(out+'\n') + + if(use_lf): + fp.close() + + +############################################# +## Compiler, Archive, and Linker Functions ## +############################################# + +#MSVC compiler wrapper +def msvc_compile(compilername, srcfile, **kwargs): + + if(not('include' in kwargs)): + include = '' + else: + include = kwargs['include'] + if(isinstance(include,list)): + include = list_to_sss(include) + + if(not('flags' in kwargs)): + flags = '' + else: + flags = kwargs['flags'] + if(isinstance(flags,list)): + flags = list_to_sss(flags) + + if(not('objext' in kwargs)): + objext = '.obj' + else: + objext = kwargs['objext'] + + if(not('srcfileflag' in kwargs)): + srcfileflag = '/c' + else: + srcfileflag = kwargs['srcfileflag'] + + if(not('outfileflag' in kwargs)): + outfileflag = '/Fo:' + else: + outfileflag = kwargs['outfileflag'] + if(not('logfile' in kwargs)): + logfile = "" + else: + logfile = kwargs['logfile'] + + outfile = replaceext(srcfile,objext) + ln = compilername+" "+flags+" "+" "+srcfileflag+" "+srcfile+" "+outfileflag+'"'+outfile+'"' + ln = ln + " " + include + + callproc(ln,echo=True,logfile=logfile) + + return + +#MSVC compiler wrapper +def msvc_compile_list(compiler,srclist,**kwargs): + for S in srclist: + msvc_compile(compiler,S,**kwargs) + return + +#gnu-style compiler compile: Should work with gcc, g++, gfortran +def gs_compile(compiler,srcfile,**kwargs): + if(not('include' in kwargs)): + include = '' + else: + include = kwargs['include'] + if(isinstance(include,list)): + include = list_to_sss(include) + + if(not('flags' in kwargs)): + flags = '' + else: + flags = kwargs['flags'] + if(isinstance(flags,list)): + flags = list_to_sss(flags) + + if(not('objext' in kwargs)): + objext = '.o' + else: + objext = kwargs['objext'] + + if(not('srcfileflag' in kwargs)): + srcfileflag = '-c' + else: + srcfileflag = kwargs['srcfileflag'] + + if(not('outfileflag' in kwargs)): + outfileflag = '-o' + else: + outfileflag = kwargs['outfileflag'] + + if(not('logfile' in kwargs)): + logfile = "" + else: + logfile = kwargs['logfile'] + + if(not('smartcompile' in kwargs)): + _smartcompile = True + else: + _smartcompile = kwargs['smartcompile'] + + #Do I want to make this thing this general? + + # if(not(_smartcompile) or smartcompile(srcfile,objext)): + # outfile = replaceext(srcfile,objext) + # ln = compiler+" "+flags+" " + outfileflag+" "+outfile+" "+srcfileflag+" "+srcfile + # ln = ln + " " + include + + # callproc(ln,echo=True,logfile=logfile) + + outfile = replaceext(srcfile,objext) + ln = compiler+" "+flags+" " + outfileflag+" "+outfile+" "+srcfileflag+" "+srcfile + ln = ln + " " + include + + callproc(ln,echo=True,logfile=logfile) + + return + +def gs_compile_list(compiler,srclist,**kwargs): + for S in srclist: + gs_compile(compiler,S,**kwargs) + return + +def gs_compile_all(compiler,srcdir,srcexts,**kwargs): + if(not('recurse' in kwargs)): + recurse = True + else: + recurse = kwargs['recurse'] + + srcfils = flist(srcdir,exts=srcexts,recurse=recurse) + + for S in srcfils: + gs_compile(compiler,S,**kwargs) + + return + +def gs_link_all(linker,srcpath,target,**kwargs): + + if(not('objext' in kwargs)): + objext = '.o' + else: + objext = kwargs['objext'] + + if(not('recurse' in kwargs)): + recurse = True + else: + recurse = kwargs['recurse'] + + + objfils = flist(srcpath,exts=objext,recurse=recurse) + oflst = list_to_sss(objfils) + + gs_link_list(linker,oflst,target,**kwargs) + + return + +def gs_link_list(linker,objlist,target,**kwargs): + + if(not('objext' in kwargs)): + objext = '.o' + else: + objext = kwargs['objext'] + + if(not('libdir' in kwargs)): + libdir = '' + else: + libdir = kwargs['libdir'] + + if(not('staticlibs' in kwargs)): + staticlibs = '' + else: + staticlibs = kwargs['staticlibs'] + + if(not('libflags' in kwargs)): + libflags = '' + else: + libflags = kwargs['libflags'] + + if(not('linkerflags' in kwargs)): + linkerflags = '' + else: + linkerflags = kwargs['linkerflags'] + + if(not('recurse' in kwargs)): + recurse = True + else: + recurse = kwargs['recurse'] + + if(not('logfile' in kwargs)): + logfile = '' + else: + logfile = kwargs['logfile'] + + ln = linker+" -o "+target+" "+libdir + ln = ln+" "+objlist+" "+staticlibs+" "+libflags+" "+linkerflags + + callproc(ln,logfile=logfile) + return + +def msvc_link_list(objlist,target,**kwargs): + + linker = 'link' + + if(not('objext' in kwargs)): + objext = '.obj' + else: + objext = kwargs['objext'] + + if(not('libdir' in kwargs)): + libdir = '' + else: + libdir = kwargs['libdir'] + + if(not('staticlibs' in kwargs)): + staticlibs = '' + else: + staticlibs = kwargs['staticlibs'] + + if(not('libflags' in kwargs)): + libflags = '' + else: + libflags = kwargs['libflags'] + + if(not('linkerflags' in kwargs)): + linkerflags = '' + else: + linkerflags = kwargs['linkerflags'] + + if(not('recurse' in kwargs)): + recurse = True + else: + recurse = kwargs['recurse'] + + if(not('logfile' in kwargs)): + logfile = '' + else: + logfile = kwargs['logfile'] + + ln = linker+" "+libdir + ln = ln+" "+objlist+" "+staticlibs+" "+linkerflags + ln = ln+" /out:"+target+" "+libflags + + callproc(ln,logfile=logfile) + + return + +def ar_all(srcpath,arname,**kwargs): + if(not('recurse' in kwargs)): + recurse = True + else: + recurse = kwargs['recurse'] + if(not('objext' in kwargs)): + objext = '.o' + else: + objext = kwargs['objext'] + + objlist = flist(srcpath,exts=objext,recurse=recurse) + ar_list(objlist,arname,**kwargs) + + return + +def msvc_lib_list(objlist,arname,**kwargs): + objlist2 = list_to_sss(objlist) + + ln = "lib "+objlist2+" /out:"+arname + callproc(ln) + + return + +def ar_list(objlist,arname,**kwargs): + objlist2 = list_to_sss(objlist) + + ln = "ar cr "+ arname+" "+objlist2 + callproc(ln) + + return + +def ar_add_list(objlist,arname,**kwargs): + objlist2 = list_to_sss(objlist) + + ln = "ar t "+arname+" "+objlist2 + callproc(ln) + return + +##################################### +## Incremental Compilation Library ## +##################################### + +#silently read lines from a text file if exists +def readtextlines(fname): + txtlns = [] + + if(not os.path.isfile(fname)): + return txtlns + + try: + fp = open(fname,"r") + except: + return txtlns + + ln = " " + while(ln!=""): + ln = fp.readline() + txtlns.append(ln) + + fp.close() + + return txtlns + +def getincludefnfrage(includeline): + + fnfrag = "" + I1 = -1 + I2 = -1 + + for I in range(0,len(includeline)): + if(I1<0 and (includeline[I]=='<' or includeline[I]=='"')): + I1 = I + if(I1>=0 and (includeline[I]=='>' or includeline[I]=='"')): + I2 = I + break + if(I1>=0 and I2>=0): + fnfrag = includeline[I1+1:I2] + + return fnfrag + +#Returns the name of the source file fname (if it exists) +#and all included filenames +def getsrcandincludes(fname, incdirs): + + flist = [] + if(os.path.isfile(fname)): + flist.append(fname) + + Ilist = 0 + while(Ilist=0): + fnfrag = getincludefnfrage(lns[J]) + for K in range(0,len(incdirs)): + tfn = os.path.join(incdirs[K],fnfrag) + if(os.path.isfile(tfn)): + flist.append(tfn) + break + + Ilist = Ilist + 1 + + return flist + +#Returns the name of the object file associated with the source file +#within the object store folder (if it exists) +def getobjfile(fname,objstore,objext = ".o"): + + fret = "" + f1 = os.path.split(fname)[1] + f2 = f1 + while(os.path.splitext(f2)[1]!=""): + f2 = os.path.splitext(f2)[0] + objext = objext.strip('.') + f3 = os.path.join(objstore,"{}.{}".format(f2,objext)) + if(os.path.exists(f3)): + fret = f3 + + return fret + +def getsrctimes(fname, incdirs): + + ftimes = [] + flst = getsrcandincludes(fname, incdirs) + for I in range(0,len(flst)): + f = flst[I] + mt = os.path.getmtime(f) + ftimes.append(mt) + + return ftimes + +def getobjtime(fname,objstore,objext=".o"): + ret = -1 + fret = getobjfile(fname,objstore,objext) + if(fret!=""): + ret = os.path.getmtime(fret) + + return ret + +#Decide whether or not to compile source file +def decidecompile(fname,**kwargs): + ret = True + + if(not os.path.isfile(fname)): + ret = False + return ret + + ##unpack kwargs + if("searchincdirs" in kwargs): + incdirs = kwargs["searchincdirs"] + else: + incdirs = ["./include"] + + if("objext" in kwargs): + objext = kwargs["objext"] + else: + objext = ".o" + if("objstore" in kwargs): + objstore = kwargs["objstore"] + else: + objstore = "./objstore" + + + srclist = getsrcandincludes(fname,incdirs) + srctlist = getsrctimes(fname,incdirs) + obj = getobjfile(fname,objstore,objext) + objt = getobjtime(fname,objstore,objext) + + if(obj!=""): + ret = False + for I in range(0,len(srctlist)): + if(srctlist[I]>objt): + ret = True + break + + return ret + +def gs_incremental_compile(compiler,srcfile,**kwargs): + + if(not('include' in kwargs)): + include = '' + else: + include = kwargs['include'] + if(isinstance(include,list)): + include = list_to_sss(include) + if(not('flags' in kwargs)): + flags = '' + else: + flags = kwargs['flags'] + if(isinstance(flags,list)): + flags = list_to_sss(flags) + if(not('objext' in kwargs)): + objext = '.o' + else: + objext = kwargs['objext'] + if(not('srcfileflag' in kwargs)): + srcfileflag = '-c' + else: + srcfileflag = kwargs['srcfileflag'] + if(not('outfileflag' in kwargs)): + outfileflag = '-o' + else: + outfileflag = kwargs['outfileflag'] + + if(not('logfile' in kwargs)): + logfile = "" + else: + logfile = kwargs['logfile'] + if(not('smartcompile' in kwargs)): + _smartcompile = True + else: + _smartcompile = kwargs['smartcompile'] + + #incrementalcompile + if("searchincdirs" in kwargs): + incdirs = kwargs["searchincdirs"] + else: + incdirs = ["./include"] + + if("objstore" in kwargs): + objstore = kwargs["objstore"] + else: + objstore = "./objstore" + + #Do I want to make this thing this general? + + docompile = decidecompile(srcfile,**kwargs) + + if(docompile): + f1 = os.path.split(srcfile)[1] + f2 = f1 + while(os.path.splitext(f2)[1]!=""): + f2 = os.path.splitext(f2)[0] + outfile = os.path.join(objstore,"{}{}".format(f2,objext)) + + ln = compiler+" "+flags+" " + outfileflag+" "+outfile+" "+srcfileflag+" "+srcfile + ln = ln + " " + include + + callproc(ln,echo=True,logfile=logfile) + + return + +def msvc_incremental_compile(compiler,srcfile,**kwargs): + + if(not('include' in kwargs)): + include = '' + else: + include = kwargs['include'] + if(isinstance(include,list)): + include = list_to_sss(include) + if(not('flags' in kwargs)): + flags = '' + else: + flags = kwargs['flags'] + if(isinstance(flags,list)): + flags = list_to_sss(flags) + if(not('objext' in kwargs)): + objext = '.obj' + else: + objext = kwargs['objext'] + if(not('srcfileflag' in kwargs)): + srcfileflag = '/c' + else: + srcfileflag = kwargs['srcfileflag'] + if(not('outfileflag' in kwargs)): + outfileflag = '/Fo' + else: + outfileflag = kwargs['outfileflag'] + + if(not('logfile' in kwargs)): + logfile = "" + else: + logfile = kwargs['logfile'] + + #incrementalcompile + if("searchincdirs" in kwargs): + incdirs = kwargs["searchincdirs"] + else: + incdirs = ["./include"] + + if("objstore" in kwargs): + objstore = kwargs["objstore"] + else: + objstore = "./objstore" + + #Do I want to make this thing this general? + + docompile = decidecompile(srcfile,**kwargs) + + if(docompile): + f1 = os.path.split(srcfile)[1] + f2 = f1 + while(os.path.splitext(f2)[1]!=""): + f2 = os.path.splitext(f2)[0] + outfile = os.path.join(objstore,"{}{}".format(f2,objext)) + outfile = os.path.normpath(outfile) + + ln = compiler+" "+flags+" "+srcfileflag+" "+srcfile+" "+ outfileflag+'"'+outfile+'"' + ln = ln + " " + include + + callproc(ln,echo=True,logfile=logfile) + + return + + +def gs_incremental_compile_list(compiler,srclist,**kwargs): + + for s in srclist: + gs_incremental_compile(compiler,s,**kwargs) + + return + +def msvc_incremental_compile_list(compiler,srclist,**kwargs): + + for s in srclist: + msvc_incremental_compile(compiler,s,**kwargs) + + return + +####################### +## Main Script Tests ## +####################### + +def testtimes(args): + if(len(args)>=2): + flist = getsrcandincludes(args[1],["./include"]) + ftlist = getsrctimes(args[1],["./include"]) + for I in range(0,len(flist)): + print("{}\t\t{}".format(flist[I],ftlist[I])) + + print("associated obj file:") + fobj = getobjfile(args[1],"./objstore") + ftobj = getobjtime(args[1],"./objstore") + if(fobj!=""): + print("{}\t\t{}".format(fobj,ftobj)) + else: + print("none found") + + cflag = decidecompile(args[1]) + print("compile? : {}".format(cflag)) + + + return + +# if(__name__ == "__main__"): + +# args = sys.argv +# testtimes(args) + + + + \ No newline at end of file diff --git a/build/make.linux64.lib.py b/build/make.linux64.lib.py new file mode 100644 index 0000000..5237568 --- /dev/null +++ b/build/make.linux64.lib.py @@ -0,0 +1,56 @@ +#!/usr/bin/python3 + +import os,sys,math +import subprocess +import shutil +from shutil import copytree + +from amsbuildlib4 import * + +libname = "amscppnarray25.linux64" #static library name to generate +binname = "tests" #create this executable when compiling main.c or main.cpp +commondir = "../../linux64" #common directory to pul libraries and includes from +installdir = "../../linux64" #directory to install to when finished +builddir = "./build_linux64" + +doinstall = True #copies the build_output to the install dir when finished +cc = "g++" #compiler +cflags = "-fPIC -O3" +libraries = "-l{}".format(libname) +libdirs = "-L{} -L{}/lib".format(builddir,commondir) +linkerflags = "-static -static-libgcc -Wl,-rpath=." +srcexts = [".c",".cpp"] +binsrc = ["main.c","main.cpp"] #ignore these files when compiling the static library + +#keyword list to control the compilers/linkers +kwargs = dict() +include = "-I./include -I{}/include".format(commondir) +kwargs["include"] = include +kwargs["flags"] = cflags +kwargs["libdir"] = libdirs +kwargs["libflags"] = libraries +kwargs["linkerflags"] = linkerflags +kwargs["recurse"] = True +kwargs["objstore"] = "{}/objstore".format(builddir) +kwargs["searchincdirs"] = "./include" + +#Find all source files, except the main project files +srcfiles = flist('./src',exts = srcexts, recurse=True) +srcfiles = except_contains(srcfiles,binsrc) + +#compile all the source files in the list +#gs_compile_list(cc,files,**kwargs) +gs_incremental_compile_list(cc,srcfiles,**kwargs) + +#archive all the source files into a static library +objlist = flist(kwargs['objstore'],exts='.o',recurse=True) +ar_list(objlist,'{}/lib{}.a'.format(builddir,libname)) + +if(doinstall): + #Copy a binary to the common bin folder + + #Push any libraries to the common lib folder + shutil.copy('{}/lib{}.a'.format(builddir,libname),"{}/lib".format(commondir)) + + #Copy include files to the common include folder + copytree('./include/',commondir+'/include/',dirs_exist_ok=True) diff --git a/build/make.linux64.test.py b/build/make.linux64.test.py new file mode 100644 index 0000000..0683d98 --- /dev/null +++ b/build/make.linux64.test.py @@ -0,0 +1,48 @@ +#!/usr/bin/python3 + +import os,sys,math +import subprocess +import shutil +from shutil import copytree + +from amsbuildlib4 import * + +libname = "amscppnarray25.linux64" #static library name to generate +binname = "tests" #create this executable when compiling main.c or main.cpp +commondir = "../../linux64" #common directory to pul libraries and includes from +installdir = "../../linux64" #directory to install to when finished +builddir = "./build_linux64" + +doinstall = True #copies the build_output to the install dir when finished +cc = "g++" #compiler +cflags = "-fPIC" +libraries = "-l{} -lamsmathutil25.linux64".format(libname) +libdirs = "-L{} -L{}/lib".format(builddir,commondir) +linkerflags = "-static -static-libgcc -Wl,-rpath=." +srcexts = [".c",".cpp"] +binsrc = ["main.c","main.cpp"] #ignore these files when compiling the static library + +#keyword list to control the compilers/linkers +kwargs = dict() +include = "-I./include -I{}/include".format(commondir) +kwargs["include"] = include +kwargs["flags"] = cflags +kwargs["libdir"] = libdirs +kwargs["libflags"] = libraries +kwargs["linkerflags"] = linkerflags +kwargs["recurse"] = True +kwargs["objstore"] = "{}/objstore".format(builddir) +kwargs["searchincdirs"] = "./include" + +#Pull required binary dynamic libraries to the bin folder +#shutil.copy('{}/lib/libcamsimg3.linux64.so'.format(commondir),builddir); +#shutil.copy('{}/lib/libamsimg.dll'.format(commondir),builddir); +#shutil.copy('{}/lib/glew32.dll','./bin_winx64'); + +#Designate source files for main test program +fsrc = ['./src/main.cpp'] +fobj = replaceexts(fsrc,'.o') + +#Compile test programs +gs_compile_list(cc,fsrc,**kwargs) +gs_link_list(cc,list_to_sss(fobj),'{}/{}'.format(builddir,binname),**kwargs) diff --git a/build/make.mingw64.lib.py b/build/make.mingw64.lib.py new file mode 100644 index 0000000..8d576a0 --- /dev/null +++ b/build/make.mingw64.lib.py @@ -0,0 +1,58 @@ +#!/usr/bin/python3 + +import os,sys,math +import subprocess +import shutil +from shutil import copytree + +from amsbuildlib4 import * + +libname = "amscppnarray25.mingw64" #static library name to generate +binname = "tests" #create this executable when compiling main.c or main.cpp +commondir = "../../winx64" #common directory to pul libraries and includes from +depdir = "./dependencies/winx64" #local pre-compiled dependency libraries and their includes +installdir = "../../winx64" #directory to install to when finished +builddir = "./build_mingw64" + +doinstall = True #copies the build_output to the install dir when finished +cc = "x86_64-w64-mingw32-g++" #compiler +cflags = "-fPIC -O3" +libraries = "-l{}".format(libname) +libdirs = "-L{} -L{}/lib -L{}/lib".format(builddir,commondir,depdir) +linkerflags = "-static -static-libgcc -Wl,-rpath=." +srcexts = [".c",".cpp"] +binsrc = ["main.c","main.cpp"] #ignore these files when compiling the static library + + +#keyword list to control the compilers/linkers +kwargs = dict() +include = "-I./include -I{}/include -I{}/include".format(commondir, depdir) +kwargs["include"] = include +kwargs["flags"] = cflags +kwargs["libdir"] = libdirs +kwargs["libflags"] = libraries +kwargs["linkerflags"] = linkerflags +kwargs["recurse"] = True +kwargs["objstore"] = "{}/objstore".format(builddir) +kwargs["searchincdirs"] = "./include" + +#Find all source files, except the main project files +srcfiles = flist('./src',exts = srcexts, recurse=True) +srcfiles = except_contains(srcfiles,binsrc) + +#compile all the source files in the list +#gs_compile_list(cc,files,**kwargs) +gs_incremental_compile_list(cc,srcfiles,**kwargs) + +#archive all the source files into a static library +objlist = flist(kwargs['objstore'],exts='.o',recurse=True) +ar_list(objlist,'{}/lib{}.a'.format(builddir,libname)) + +if(doinstall): + #Copy a binary to the common bin folder + + #Push any libraries to the common lib folder + shutil.copy('{}/lib{}.a'.format(builddir,libname),"{}/lib".format(commondir)) + + #Copy include files to the common include folder + copytree('./include/',commondir+'/include/',dirs_exist_ok=True) diff --git a/build/make.mingw64.test.py b/build/make.mingw64.test.py new file mode 100644 index 0000000..00d184f --- /dev/null +++ b/build/make.mingw64.test.py @@ -0,0 +1,49 @@ +#!/usr/bin/python3 + +import os,sys,math +import subprocess +import shutil +from shutil import copytree + +from amsbuildlib4 import * + +libname = "amscppnarray25.mingw64" #static library name to generate +binname = "tests" #create this executable when compiling main.c or main.cpp +commondir = "../../winx64" #common directory to pul libraries and includes from +depdir = "./dependencies/winx64" #local pre-compiled dependency libraries and their includes +installdir = "../../winx64" #directory to install to when finished +builddir = "./build_mingw64" + +doinstall = False #copies the build_output to the install dir when finished +cc = "x86_64-w64-mingw32-g++" #compiler +cflags = "-fPIC -O3" +libraries = "-l{} -lamsmathutil25.mingw64".format(libname) +libdirs = "-L{} -L{}/lib -L{}/lib".format(builddir,commondir,depdir) +linkerflags = "-static -static-libgcc -Wl,-rpath=." +srcexts = [".c",".cpp"] +binsrc = ["main.c","main.cpp"] #ignore these files when compiling the static library + +#keyword list to control the compilers/linkers +kwargs = dict() +include = "-I./include -I{}/include -I{}/include".format(commondir, depdir) +kwargs["include"] = include +kwargs["flags"] = cflags +kwargs["libdir"] = libdirs +kwargs["libflags"] = libraries +kwargs["linkerflags"] = linkerflags +kwargs["recurse"] = True +kwargs["objstore"] = "{}/objstore".format(builddir) +kwargs["searchincdirs"] = "./include" + +#Pull required binary dynamic libraries to the bin folder +#shutil.copy('{}/lib/libcamsimg3.linux64.so'.format(commondir),builddir); +#shutil.copy('{}/lib/libamsimg.dll'.format(commondir),builddir); +#shutil.copy('{}/lib/glew32.dll','./bin_winx64'); + +#Designate source files for main test program +fsrc = ['./src/main.cpp'] +fobj = replaceexts(fsrc,'.o') + +#Compile test programs +gs_compile_list(cc,fsrc,**kwargs) +gs_link_list(cc,list_to_sss(fobj),'{}/{}'.format(builddir,binname),**kwargs) diff --git a/build/make.msvc64.lib.py b/build/make.msvc64.lib.py new file mode 100644 index 0000000..65f2f85 --- /dev/null +++ b/build/make.msvc64.lib.py @@ -0,0 +1,60 @@ +#!/usr/bin/python3 + +import os,sys,math +import subprocess +import shutil +from shutil import copytree + +from amsbuildlib4 import * + +libname = "amscppnarray25.msvc64" #static library name to generate +binname = "tests.exe" #create this executable when compiling main.c or main.cpp +commondir = "../../winx64" #common directory to pul libraries and includes from +depdir = "./dependencies/winx64" #local pre-compiled dependency libraries and their includes +installdir = "../../winx64" #directory to install to when finished +builddir = "./build_msvc64" + +doinstall = True #copies the build_output to the install dir when finished +cc = "cl" #compiler +cflags = "/O2 /EHsc" +libraries = "lib{}.lib libamsmathutil2.msvc64.lib".format(libname) +libdirs = "/LIBPATH:{} /LIBPATH:{}/lib /LIBPATH:{}/lib".format(builddir,commondir,depdir) +linkerflags = "-static -static-libgcc -Wl,-rpath=." +srcexts = [".c",".cpp"] +binsrc = ["main.c","main.cpp"] #ignore these files when compiling the static library + + +#keyword list to control the compilers/linkers +kwargs = dict() +include = "-I./include -I{}/include -I{}/include".format(commondir, depdir) +kwargs["include"] = include +kwargs["flags"] = cflags +kwargs["libdir"] = libdirs +kwargs["libflags"] = libraries +kwargs["linkerflags"] = linkerflags +kwargs["recurse"] = True +kwargs["objstore"] = "{}/objstore".format(builddir) +kwargs["searchincdirs"] = "./include" +kwargs["objext"] = ".obj" + + +#Find all source files, except the main project files +srcfiles = flist('./src',exts = srcexts, recurse=True) +srcfiles = except_contains(srcfiles,binsrc) + +#compile all the source files in the list +#gs_compile_list(cc,files,**kwargs) +msvc_incremental_compile_list(cc,srcfiles,**kwargs) + +#archive all the source files into a static library +objlist = flist(kwargs['objstore'],exts='.obj',recurse=True) +msvc_lib_list(objlist,'{}/lib{}.lib'.format(builddir,libname)) + +if(doinstall): + #Copy a binary to the common bin folder + + #Push any libraries to the common lib folder + shutil.copy('{}/lib{}.lib'.format(builddir,libname),"{}/lib".format(commondir)) + + #Copy include files to the common include folder + copytree('./include/',commondir+'/include/',dirs_exist_ok=True) diff --git a/build/make.msvc64.test.py b/build/make.msvc64.test.py new file mode 100644 index 0000000..af8c008 --- /dev/null +++ b/build/make.msvc64.test.py @@ -0,0 +1,49 @@ +#!/usr/bin/python3 + +import os,sys,math +import subprocess +import shutil +from shutil import copytree + +from amsbuildlib4 import * + +libname = "amscppnarray25.msvc64" #static library name to generate +binname = "tests.exe" #create this executable when compiling main.c or main.cpp +commondir = "../../winx64" #common directory to pul libraries and includes from +depdir = "./dependencies/winx64" #local pre-compiled dependency libraries and their includes +installdir = "../../winx64" #directory to install to when finished +builddir = "./build_msvc64" + +doinstall = True #copies the build_output to the install dir when finished +cc = "cl" #compiler +cflags = "/O2 /EHsc" +libraries = "lib{}.lib libamsmathutil2.msvc64.lib".format(libname) +libdirs = "/LIBPATH:{} /LIBPATH:{}/lib /LIBPATH:{}/lib".format(builddir,commondir,depdir) +linkerflags = "-static -static-libgcc -Wl,-rpath=." +srcexts = [".c",".cpp"] +binsrc = ["main.c","main.cpp"] #ignore these files when compiling the static library + +#keyword list to control the compilers/linkers +kwargs = dict() +include = "-I./include -I{}/include -I{}/include".format(commondir, depdir) +kwargs["include"] = include +kwargs["flags"] = cflags +kwargs["libdir"] = libdirs +kwargs["libflags"] = libraries +kwargs["linkerflags"] = linkerflags +kwargs["recurse"] = True +kwargs["objstore"] = "{}/objstore".format(builddir) +kwargs["searchincdirs"] = "./include" + +#Pull required binary dynamic libraries to the bin folder +#shutil.copy('{}/lib/libcamsimg3.linux64.so'.format(commondir),builddir); +#shutil.copy('{}/lib/libamsimg.dll'.format(commondir),builddir); +#shutil.copy('{}/lib/glew32.dll','./bin_winx64'); + +#Designate source files for main test program +fsrc = ['./src/main.cpp'] +fobj = replaceexts(fsrc,'.obj') + +#Compile test programs +msvc_compile_list(cc,fsrc,**kwargs) +msvc_link_list(list_to_sss(fobj),'{}/{}'.format(builddir,binname),**kwargs) diff --git a/build_linux64/libamscppnarray.linux64.a b/build_linux64/libamscppnarray.linux64.a new file mode 100644 index 0000000..1778633 Binary files /dev/null and b/build_linux64/libamscppnarray.linux64.a differ diff --git a/build_linux64/libamscppnarray25.linux64.a b/build_linux64/libamscppnarray25.linux64.a new file mode 100644 index 0000000..e72b947 Binary files /dev/null and b/build_linux64/libamscppnarray25.linux64.a differ diff --git a/build_linux64/objstore/.placeholder b/build_linux64/objstore/.placeholder new file mode 100644 index 0000000..e69de29 diff --git a/build_linux64/objstore/amscppnarray.o b/build_linux64/objstore/amscppnarray.o new file mode 100644 index 0000000..2e8b28c Binary files /dev/null and b/build_linux64/objstore/amscppnarray.o differ diff --git a/build_linux64/objstore/amscppnarray_inits.o b/build_linux64/objstore/amscppnarray_inits.o new file mode 100644 index 0000000..6ce4664 Binary files /dev/null and b/build_linux64/objstore/amscppnarray_inits.o differ diff --git a/build_linux64/objstore/amscppnarray_math.o b/build_linux64/objstore/amscppnarray_math.o new file mode 100644 index 0000000..e910f58 Binary files /dev/null and b/build_linux64/objstore/amscppnarray_math.o differ diff --git a/build_linux64/objstore/amscppnarray_random.o b/build_linux64/objstore/amscppnarray_random.o new file mode 100644 index 0000000..edb3c06 Binary files /dev/null and b/build_linux64/objstore/amscppnarray_random.o differ diff --git a/build_linux64/tests b/build_linux64/tests new file mode 100644 index 0000000..c6bc9ac Binary files /dev/null and b/build_linux64/tests differ diff --git a/build_linux64/tmp_testrand.bin b/build_linux64/tmp_testrand.bin new file mode 100644 index 0000000..003adaf Binary files /dev/null and b/build_linux64/tmp_testrand.bin differ diff --git a/build_mingw64/libamscppnarray25.mingw64.a b/build_mingw64/libamscppnarray25.mingw64.a new file mode 100644 index 0000000..63c4b28 Binary files /dev/null and b/build_mingw64/libamscppnarray25.mingw64.a differ diff --git a/build_mingw64/objstore/.placeholder b/build_mingw64/objstore/.placeholder new file mode 100644 index 0000000..e69de29 diff --git a/build_mingw64/objstore/amscppnarray.o b/build_mingw64/objstore/amscppnarray.o new file mode 100644 index 0000000..3d89e84 Binary files /dev/null and b/build_mingw64/objstore/amscppnarray.o differ diff --git a/build_mingw64/objstore/amscppnarray_inits.o b/build_mingw64/objstore/amscppnarray_inits.o new file mode 100644 index 0000000..0e38514 Binary files /dev/null and b/build_mingw64/objstore/amscppnarray_inits.o differ diff --git a/build_mingw64/objstore/amscppnarray_math.o b/build_mingw64/objstore/amscppnarray_math.o new file mode 100644 index 0000000..86b5a3d Binary files /dev/null and b/build_mingw64/objstore/amscppnarray_math.o differ diff --git a/build_mingw64/objstore/amscppnarray_random.o b/build_mingw64/objstore/amscppnarray_random.o new file mode 100644 index 0000000..cd4e06c Binary files /dev/null and b/build_mingw64/objstore/amscppnarray_random.o differ diff --git a/build_mingw64/tests.exe b/build_mingw64/tests.exe new file mode 100644 index 0000000..ca8415c Binary files /dev/null and b/build_mingw64/tests.exe differ diff --git a/build_mingw64/tmp_testrand.bin b/build_mingw64/tmp_testrand.bin new file mode 100644 index 0000000..022303d Binary files /dev/null and b/build_mingw64/tmp_testrand.bin differ diff --git a/build_msvc64/libamscppnarray.msvc64.lib b/build_msvc64/libamscppnarray.msvc64.lib new file mode 100644 index 0000000..bb6bf89 Binary files /dev/null and b/build_msvc64/libamscppnarray.msvc64.lib differ diff --git a/build_msvc64/objstore/.placeholder b/build_msvc64/objstore/.placeholder new file mode 100644 index 0000000..e69de29 diff --git a/build_msvc64/objstore/amscppnarray.obj b/build_msvc64/objstore/amscppnarray.obj new file mode 100644 index 0000000..0c666a1 Binary files /dev/null and b/build_msvc64/objstore/amscppnarray.obj differ diff --git a/build_msvc64/objstore/amscppnarray_inits.obj b/build_msvc64/objstore/amscppnarray_inits.obj new file mode 100644 index 0000000..e651932 Binary files /dev/null and b/build_msvc64/objstore/amscppnarray_inits.obj differ diff --git a/build_msvc64/objstore/amscppnarray_math.obj b/build_msvc64/objstore/amscppnarray_math.obj new file mode 100644 index 0000000..0e93352 Binary files /dev/null and b/build_msvc64/objstore/amscppnarray_math.obj differ diff --git a/build_msvc64/objstore/amscppnarray_random.obj b/build_msvc64/objstore/amscppnarray_random.obj new file mode 100644 index 0000000..6227b18 Binary files /dev/null and b/build_msvc64/objstore/amscppnarray_random.obj differ diff --git a/build_msvc64/tests b/build_msvc64/tests new file mode 100644 index 0000000..687bda0 Binary files /dev/null and b/build_msvc64/tests differ diff --git a/build_msvc64/tests.exe b/build_msvc64/tests.exe new file mode 100644 index 0000000..13ab8ac Binary files /dev/null and b/build_msvc64/tests.exe differ diff --git a/include/amscppnarray/amscppnarray.hpp b/include/amscppnarray/amscppnarray.hpp new file mode 100644 index 0000000..68d8801 --- /dev/null +++ b/include/amscppnarray/amscppnarray.hpp @@ -0,0 +1,170 @@ +#ifndef __AMSCPPNARRAY_HPP__ +#define __AMSCPPNARRAY_HPP__ + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include + +namespace ams +{ +namespace narray +{ + +// Forward declaration of the operator- template function +//template class narray; +//template narray operator-(const narray &n); + +typedef int64_t narray_size_t; + +static const int narray_success = 1; +static const int narray_failure = -1; + +//problem size at which to begin using threaded operations +static const narray_size_t narray_thread_sz = 5000; + +//maximum number of threads to use +static const int narray_max_threads = 50; + +template class narray +{ + public: + narray_size_t length; + T *data; + + narray(); + narray(const narray& other); + narray(narray&& other) noexcept; + + narray& operator=(const narray& other); + narray& operator=(narray&& other) noexcept; + + narray& operator=(const std::vector& other); + narray(const std::initializer_list initlist); + narray& operator=(const std::initializer_list initlist); + ~narray(); + + template operator narray() const; //casting datatypes + + int resize(const narray_size_t newlength); + const narray_size_t size() const; + + T& operator[](const narray_size_t ind); + const T& operator[](const narray_size_t ind) const; + T& at(const narray_size_t ind); + const T& at(const narray_size_t ind) const; + + void clear(); //erases all elements, sets size to 0 + void setall(const T& val); //sets all elements to val + + //Comparators + bool operator==(const narray& other) const; + bool operator!=(const narray& other) const; + + //Operations + narray operator+(const narray& other) const; + narray operator-(const narray& other) const; + narray operator*(const narray& other) const; + narray operator/(const narray& other) const; + narray operator+(const T val) const; + narray operator-(const T val) const; + narray operator*(const T val) const; + narray operator/(const T val) const; + template friend narray operator-(const narray &n); + narray applyfn(T (*fnptr)(T)) const; + + //min,max + T min() const; + T max() const; + + //insertions and deletions + // naive, assumes no ordering to the array + + //inserts a value at (before) index ind + //insert(0,val) would give {val, oldval0, oldval1, ...} + //insert(N,val) + int insert(const narray_size_t ind, const T val); + + //removes the value at index ind, and reduces array size by 1 + int erase(const narray_size_t ind); + + //finds the first instance of val in the array + narray_size_t find(const T val); + + //finds the next instance of val, starting consideration at indstart + narray_size_t findnext(const narray_size_t indstart, const T val); + + //finds where to insert a particular value such that the list remains ordered + narray_size_t find_insert_ordered(const T val); + + + //subarray operations + narray subarray(const narray_size_t ind1, const narray_size_t ind2) const; + + //permutations and sorting + + + //C++11 iterators + typedef T* iterator; + typedef const T* const_iterator; + iterator begin() {return data;} + iterator end() {return data + length;} + const_iterator begin() const {return data;} + const_iterator end() const {return data + length;} + const_iterator cbegin() const { return data; } + const_iterator cend() const { return data+length;} + + //printf (must be template specialized) + void print(int printstyle = 0); + +}; + +//Initializers +template narray arange(const T low, const T high, const T incr); +template narray zeros(const narray_size_t N); +template narray ones(const narray_size_t N); + +narray linspacef(const float low, const float high, narray_size_t N); +narray linspace(const double low, const double high, narray_size_t N); + +//cast/conversion + + +///////// +//Tests// +///////// + +void test_narray1(); +void test_narray2(); +void test_narray3(); + +void test_insertdelete1(); + + + + +}; //end namespace narray +}; //end namespace ams + +#include + +#include +#include +#include +#include + +#include +#include + +#include + +#endif \ No newline at end of file diff --git a/include/amscppnarray/amscppnarray_binrw.hpp b/include/amscppnarray/amscppnarray_binrw.hpp new file mode 100644 index 0000000..7b5d2b5 --- /dev/null +++ b/include/amscppnarray/amscppnarray_binrw.hpp @@ -0,0 +1,21 @@ +#ifndef __AMSCPPNARRAY_BINRW_HPP__ +#define __AMSCPPNARRAY_BINRW_HPP__ + +namespace ams +{ +namespace narray +{ + +//binary array read/write operations + +template int fwrite_ndarray(FILE *fp, narray *data, narray *shape); + +template int fread_ndarray(FILE *fp, narray *data, narray *shape); + + +}; +}; + +#include + +#endif \ No newline at end of file diff --git a/include/amscppnarray/amscppnarray_binrwimpl.hpp b/include/amscppnarray/amscppnarray_binrwimpl.hpp new file mode 100644 index 0000000..ec8c725 --- /dev/null +++ b/include/amscppnarray/amscppnarray_binrwimpl.hpp @@ -0,0 +1,153 @@ +#ifndef __AMSCPPNARRAY_BINRWIMPL_HPP__ +#define __AMSCPPNARRAY_BINRWIMPL_HPP__ + + + +namespace ams +{ +namespace narray +{ + +//binary array read/write operations + +template int fwrite_ndarray(FILE *fp, narray *data, narray *shape) +{ + int ret = narray_success; + narray_size_t piprod; + narray_size_t I; + int32_t Nd; + narray *lshape=NULL; + + if(fp==NULL) + { + ret = narray_failure; + printf("fwrite_ndarray: fp=NULL\n"); + return ret; + } + + if(shape==NULL) + { + lshape = new(std::nothrow) narray(); + lshape->resize(1); + lshape->data[0] = data->length; + } + else + { + lshape = shape; + } + + piprod = 1; + for(I=0;Ilength;I++) + { + if(lshape->data[I]>0) + { + piprod = piprod*lshape->data[I]; + } + else + { + piprod = 0; break; + } + } + + if(piprod!=data->length) + { + ret = narray_failure; + printf("fwrite_ndarray: buffer is size %ld, while shape is size %ld\n",(long) data->length,(long) piprod); + return ret; + } + + Nd = lshape->length; + + fwrite(&Nd,sizeof(int32_t),1,fp); + if(Nd>0) + { + fwrite(lshape->data,sizeof(int32_t),Nd,fp); + if(piprod>0) + { + fwrite(data->data,sizeof(T),data->length,fp); + } + } + + if(shape==NULL) + { + if(lshape!=NULL) {delete lshape; lshape=NULL;} + } + + + return ret; +} + +template int fread_ndarray(FILE *fp, narray *data, narray *shape) +{ + int ret = narray_success; + narray_size_t piprod; + narray_size_t I; + int32_t Nd; + narray *lshape=NULL; + + if(fp==NULL) + { + ret = narray_failure; + printf("fread_ndarray: fp=NULL\n"); + return ret; + } + + if(shape==NULL) + { + lshape = new(std::nothrow) narray(); + } + else + { + lshape = shape; + } + + + + fread(&Nd,sizeof(int32_t),1,fp); + if(Nd<=0) + { + data->resize(0); + lshape->resize(0); + ret = narray_failure; + return ret; + } + lshape->resize(Nd); + fread(lshape->data,sizeof(int32_t),Nd,fp); + + piprod = 1; + for(I=0;Ilength;I++) + { + if(lshape->data[I]>0) + { + piprod = piprod*lshape->data[I]; + } + else + { + piprod = 0; break; + } + } + + if(piprod<=0) + { + data->resize(0); + lshape->resize(0); + ret = narray_failure; + return ret; + } + + data->resize(piprod); + fread(data->data,sizeof(T),data->length,fp); + + if(shape==NULL) + { + if(lshape!=NULL) {delete lshape; lshape=NULL;} + } + + return ret; +} + + +}; +}; + +#endif \ No newline at end of file diff --git a/include/amscppnarray/amscppnarray_impl.hpp b/include/amscppnarray/amscppnarray_impl.hpp new file mode 100644 index 0000000..4d1f3b8 --- /dev/null +++ b/include/amscppnarray/amscppnarray_impl.hpp @@ -0,0 +1,524 @@ +#ifndef __AMSCPPNARRAY_IMPL_HPP__ +#define __AMSCPPNARRAY_IMPL_HPP__ + +namespace ams +{ +namespace narray +{ + +template narray::narray() +{ + //printf("debug: constructor called for %p.\n",this); + + length = 0; + data = NULL; + + return; +} + +template int narray::resize(const narray_size_t newlength) +{ + int ret = narray_failure; + narray_size_t I; + T *newdata = NULL; + narray_size_t ml; + + if(newlength<=0) + { + if(data!=NULL) {delete[] data; data=NULL;} + length = 0; + ret = narray_success; + return ret; + } + + newdata = new(std::nothrow) T[newlength]; + if(newdata==NULL) + { + ret = narray_failure; + return ret; + } + + //threaded initialization for speed + ml = (length(newdata,data,ml); + buffer_set(newdata,T(),length,newlength); + + // for(I=0;I narray::narray(const narray& other) +{ + narray_size_t I; + int res; + length = 0; + data = NULL; + narray_size_t ml; + + //printf("debug: copy constructor called for\n\t%p\n\t%p\n", this, &other); + + if(this!=&other) + { + res = this->resize(other.length); + if(res!=narray_success) + { + return; + } + else + { + //threaded copy for speed + ml = (length(data,other.data,ml); + // for(I=0;I narray::narray(narray&& other) noexcept +{ + //this is a move operator + //I still need to understand when these are called. + + //printf("debug: move constructor called for\n\t%p\n\t%p\n", this, &other); + + //steal the resources from other and reset other to prevent double deletion + length = other.length; + data = other.data; + + other.length = 0; + other.data = NULL; + + return; +} + +template narray& narray::operator=(narray&& other) noexcept +{ + + //printf("debug: move= called for\n\t%p\n\t%p\n", this, &other); + + if(this!=&other) + { + if(data!=NULL) {delete[] data; data=NULL;} + + length = other.length; + data = other.data; + + other.length = 0; + other.data = NULL; + } + return *this; +} + +template narray& narray::operator=(const narray& other) +{ + narray_size_t I; + int res; + narray_size_t ml; + + //printf("debug: operator= called for\n\t%p\n\t%p\n", this, &other); + + if(this!=&other) + { + res = this->resize(other.length); + if(res!=narray_success) + { + return *this; + } + else + { + //threaded copy for speed + ml = (length(data,other.data,ml); + // for(I=0;I narray& narray::operator=(const std::vector& other) +{ + narray_size_t I; + int res; + + if(this!=&other) + { + res = this->resize(other.size()); + if(res!=narray_success) + { + return *this; + } + else + { + for(I=0;I narray::narray(const std::initializer_list initlist) +{ + narray_size_t I; + int res; + length = 0; + data = NULL; + + res = this->resize(initlist.size()); + if(res!=narray_success) + { + return; + } + else + { + I = 0; + for(T elem : initlist) + { + data[I] = elem; + if(I narray& narray::operator=(const std::initializer_list initlist) +{ + narray_size_t I; + int res; + + res = this->resize(initlist.size()); + if(res!=narray_success) + { + this->length = 0; + if(this->data != NULL) {delete[] this->data; this->data=NULL;} + return *this; + } + else + { + I = 0; + for(T elem : initlist) + { + data[I] = elem; + if(I template narray::operator narray() const +{ + //casting datatypes + narray ret; + int res; + + res = ret.resize(this->length); + if(res!=narray_success) + { + ret.resize(0); + return ret; + } + else + { + buffer_copycast(ret.data,data,length); + } + + return ret; +} + + + +template narray::~narray() +{ + //printf("debug: destructor called for %p\n",this); + length = 0; + if(data!=NULL) {delete[] data; data=NULL;} + return; +} + +template T& narray::operator[](const narray_size_t ind) +{ + return data[ind]; +} + +template const T& narray::operator[](const narray_size_t ind) const +{ + return data[ind]; +} + +template T& narray::at(const narray_size_t ind) +{ + return data[ind]; +} + +template const T& narray::at(const narray_size_t ind) const +{ + return data[ind]; +} + +template void narray::clear() +{ + this->resize(0); + return; +} + +template void narray_setall_tf(narray *q, const T& val, int threadnum, int nthreads) +{ + int I0,I1,Is,I; + + Is = q->length/nthreads; + I0 = Is*threadnum; + I1 = (threadnum>=(nthreads-1))? q->length: Is*(threadnum+1); + for(I=I0;Idata[I] = val; + } + + return; +} + +template void narray::setall(const T& val) +{ + narray_size_t I; + int J; + int nthreads; + std::vector threads; + + if(lengthnarray_max_threads) nthreads = narray_max_threads; + threads.resize(nthreads); + for(J=0;J,this,val,J,nthreads + ); + } + for(J=0;Jjoin(); + delete threads[J]; + threads[J]= NULL; + } + } + } + return; +} + +template const narray_size_t narray::size() const +{ + return length; +} + +//inserts a value at (before) index ind +//insert(0,val) would give {val, oldval0, oldval1, ...} +//insert(N,val) +template int narray::insert(const narray_size_t ind, const T val) +{ + int ret = narray_success; + int res; + narray narr; + narray_size_t I; + + if(ind<0) + { + ret = narray_failure; + return ret; + } + else if(ind<=this->length) + { + res = narr.resize(this->length+1); + if(res!=narray_success) + { + ret = narray_failure; + return ret; + } + + if(this->data!=NULL) + { + for(I=0;Ilength;I++) + { + narr.data[I] = this->data[I]; + } + } + narr.data[ind] = val; + if(this->data!=NULL) + { + for(I=ind+1;Idata[I-1]; + } + } + + //steal data from narr + if(this->data!=NULL) {delete[] this->data; this->data=NULL;} + this->length = narr.length; + this->data = narr.data; + narr.length = 0; + narr.data = NULL; + } + else + { + //inserting past the end of the array + res = narr.resize(ind+1); + if(res!=narray_success) + { + ret = narray_failure; + return ret; + } + + if(this->data!=NULL) + { + for(I=0;Ilength;I++) + { + narr.data[I] = this->data[I]; + } + } + narr.data[ind] = val; + + //steal data from narr + if(this->data!=NULL) {delete[] this->data; this->data=NULL;} + this->length = narr.length; + this->data = narr.data; + narr.length = 0; + narr.data = NULL; + } + + return ret; +} + +//removes the value at index ind, and reduces array size by 1 +template int narray::erase(const narray_size_t ind) +{ + int ret = narray_success; + int res; + narray narr; + narray_size_t I; + + if(ind<0 || ind>=this->length) + { + ret = narray_failure; + return ret; + } + + res = narr.resize(this->length-1); + if(res!=narray_success) + { + ret = narray_failure; + return ret; + } + + if(this->data!=NULL) + { + for(I=0;Ilength && Idata[I]; + } + for(I=ind+1;Ilength && (I-1)data[I]; + } + } + + //steal data from narr (should be similar to what std::move would do?) + if(this->data!=NULL) {delete[] this->data; this->data=NULL;} + this->length = narr.length; + this->data = narr.data; + narr.length = 0; + narr.data = NULL; + + return ret; +} + +//finds the first instance of val in the array +template narray_size_t narray::find(const T val) +{ + narray_size_t ret = -1; + narray_size_t I; + + for(I=0;Ilength;I++) + { + if(this->data[I]==val) + { + ret = I; + break; + } + } + + return ret; +} + +//finds the next instance of val, starting consideration at indstart +template narray_size_t narray::findnext(const narray_size_t indstart, const T val) +{ + narray_size_t ret = -1; + narray_size_t I; + + for(I=indstart;Ilength;I++) + { + if(this->data[I]==val) + { + ret = I; + break; + } + } + + return ret; +} + +//finds where to insert a particular value such that the list remains ordered +template narray_size_t narray::find_insert_ordered(const T val) +{ + narray_size_t ret = -1; + narray_size_t I; + + ret = 0; + for(I=0;Ilength;I++) + { + ret = I+1; + if(this->data[I]>val) + { + ret = I; + break; + } + } + ret = (ret<0) ? 0 : ret; + ret = (ret>this->length) ? this->length : ret; + + + return ret; +} + + +}; //end namespace narray +}; //end namespace ams + +#endif \ No newline at end of file diff --git a/include/amscppnarray/amscppnarray_implinits.hpp b/include/amscppnarray/amscppnarray_implinits.hpp new file mode 100644 index 0000000..1f20dd7 --- /dev/null +++ b/include/amscppnarray/amscppnarray_implinits.hpp @@ -0,0 +1,108 @@ +#ifndef __AMSCPPNARRAY_IMPLINITS_HPP__ +#define __AMSCPPNARRAY_IMPLINITS_HPP__ + +namespace ams +{ +namespace narray +{ + + template void narray_arange_tf( + narray *in, + const T low, const T high, const T incr, + int threadnum, int nthreads + ) + { + int I0,I1,Is,I; + Is = in->length/nthreads; + I0 = Is*threadnum; + I1 = (threadnum>=(nthreads-1))? in->length: Is*(threadnum+1); + for(I=I0;Idata[I] = low + incr*I; + } + } + +template narray arange(const T low, const T high, const T incr) +{ + narray ret; + narray_size_t I; + int J; + int nthreads; + std::vector threads; + + narray_size_t N = (narray_size_t)((high-low)/incr); + if(N<=0) + { + N = 0; + } + ret.resize(N); + + if(ret.length==0) + { + return ret; + } + + if(ret.lengthnarray_max_threads) ? narray_max_threads : nthreads; + //if(nthreads<1) nthreads=1; + //if(nthreads>narray_max_threads) nthreads = narray_max_threads; + threads.resize(nthreads); + for(J=0;J, + &ret, + low,high,incr, + J,nthreads + ); + } + for(J=0;Jjoin(); + delete threads[J]; + threads[J]= NULL; + } + } + } + + + + return ret; +} + +template narray zeros(const narray_size_t N) +{ + narray ret; + ret.resize(N); + ret.setall(0); + + return ret; +} + +template narray ones(const narray_size_t N) +{ + narray ret; + ret.resize(N); + ret.setall(1); + + return ret; +} + + +}; +}; + +#endif \ No newline at end of file diff --git a/include/amscppnarray/amscppnarray_imploper.hpp b/include/amscppnarray/amscppnarray_imploper.hpp new file mode 100644 index 0000000..d62eecf --- /dev/null +++ b/include/amscppnarray/amscppnarray_imploper.hpp @@ -0,0 +1,977 @@ +#ifndef __AMSCPPNARRAY_IMPLOPER_HPP__ +#define __AMSCPPNARRAY_IMPLOPER_HPP__ + +namespace ams +{ +namespace narray +{ + + template void narray_operator_comp_tf( + const narray *a, + const narray *b, + narray *cmp, + int threadnum, int nthreads + ) + { + int I0,I1,Is,I; + + Is = (nthreads<=1) ? a->length : a->length/nthreads; + I0 = Is*threadnum; + I1 = (threadnum>=(nthreads-1))? a->length: Is*(threadnum+1); + + cmp->data[threadnum] = 1; + for(I=I0;Idata[I]!=b->data[I]) + { + cmp->data[threadnum] = 0; + break; + } + } + return; + } + + template bool narray::operator==(const narray& other) const + { + bool ret = 1; + narray cmp; + narray_size_t I; + int J; + int nthreads; + std::vector threads; + + if(this->length != other.length) + { + ret = 0; + return ret; + } + if(this->length == 0) + { + ret = 1; + return ret; + } + if(this->length < narray_thread_sz) + { + for(I=0;Ilength;I++) + { + if(this->data[I]!=other.data[I]) + { + ret = 0; + break; + } + } + } + else + { + //threaded operation + nthreads = std::thread::hardware_concurrency(); + nthreads = (nthreads<1) ? 1 : nthreads; + nthreads = (nthreads>narray_max_threads) ? narray_max_threads : nthreads; + //if(nthreads<1) nthreads=1; + //if(nthreads>narray_max_threads) nthreads = narray_max_threads; + threads.resize(nthreads); + cmp.resize(nthreads); + for(J=0;J, + this, + &other, + &cmp, + J,nthreads + ); + } + for(J=0;Jjoin(); + delete threads[J]; + threads[J]= NULL; + } + } + + ret = 1; + for(J=0;J bool narray::operator!=(const narray& other) const + { + return !(*this==other); + } + + template void narray_operator_add_tf( + const narray *a, + const narray *b, + narray *out, + int threadnum, int nthreads + ) + { + int I0,I1,Is,I; + + Is = a->length/nthreads; + I0 = Is*threadnum; + I1 = (threadnum>=(nthreads-1))? a->length: Is*(threadnum+1); + for(I=I0;Idata[I] = a->data[I] + b->data[I]; + } + + return; + } + + template narray narray::operator+(const narray& other) const + { + narray ret; + narray_size_t I; + int J; + int nthreads; + std::vector threads; + + if(this->length!=other.length) + { + ret.resize(0); + printf("narray::operator+:: error - array sizes %ld and %ld are not the same.\n",(long)this->length,(long)other.length); + } + + ret.resize(this->length); + + if(this->lengthlength;I++) ret.data[I] = this->data[I] + other.data[I]; + } + else + { + //threaded operation + nthreads = std::thread::hardware_concurrency(); + nthreads = (nthreads<1) ? 1 : nthreads; + nthreads = (nthreads>narray_max_threads) ? narray_max_threads : nthreads; + //if(nthreads<1) nthreads=1; + //if(nthreads>narray_max_threads) nthreads = narray_max_threads; + threads.resize(nthreads); + for(J=0;J, + this, + &other, + &ret, + J,nthreads + ); + } + for(J=0;Jjoin(); + delete threads[J]; + threads[J]= NULL; + } + } + } + return ret; + } + + template void narray_operator_sub_tf( + const narray *a, + const narray *b, + narray *out, + int threadnum, int nthreads + ) + { + int I0,I1,Is,I; + + Is = a->length/nthreads; + I0 = Is*threadnum; + I1 = (threadnum>=(nthreads-1))? a->length: Is*(threadnum+1); + for(I=I0;Idata[I] = a->data[I] - b->data[I]; + } + + return; + } + + template narray narray::operator-(const narray& other) const + { + narray ret; + narray_size_t I; + int J; + int nthreads; + std::vector threads; + + if(this->length!=other.length) + { + ret.resize(0); + printf("narray::operator-:: error - array sizes %ld and %ld are not the same.\n",(long)this->length,(long)other.length); + } + + ret.resize(this->length); + + if(this->lengthlength;I++) ret.data[I] = this->data[I] - other.data[I]; + } + else + { + //threaded operation + nthreads = std::thread::hardware_concurrency(); + nthreads = (nthreads<1) ? 1 : nthreads; + nthreads = (nthreads>narray_max_threads) ? narray_max_threads : nthreads; + //if(nthreads<1) nthreads=1; + //if(nthreads>narray_max_threads) nthreads = narray_max_threads; + threads.resize(nthreads); + for(J=0;J, + this, + &other, + &ret, + J,nthreads + ); + } + for(J=0;Jjoin(); + delete threads[J]; + threads[J]= NULL; + } + } + } + return ret; + } + + template void narray_operator_mult_tf( + const narray *a, + const narray *b, + narray *out, + int threadnum, int nthreads + ) + { + int I0,I1,Is,I; + + Is = a->length/nthreads; + I0 = Is*threadnum; + I1 = (threadnum>=(nthreads-1))? a->length: Is*(threadnum+1); + for(I=I0;Idata[I] = a->data[I] * b->data[I]; + } + + return; + } + + template narray narray::operator*(const narray& other) const + { + narray ret; + narray_size_t I; + int J; + int nthreads; + std::vector threads; + + if(this->length!=other.length) + { + ret.resize(0); + printf("narray::operator*:: error - array sizes %ld and %ld are not the same.\n",(long)this->length,(long)other.length); + } + + ret.resize(this->length); + + if(this->lengthlength;I++) ret.data[I] = this->data[I] * other.data[I]; + } + else + { + //threaded operation + nthreads = std::thread::hardware_concurrency(); + nthreads = (nthreads<1) ? 1 : nthreads; + nthreads = (nthreads>narray_max_threads) ? narray_max_threads : nthreads; + //if(nthreads<1) nthreads=1; + //if(nthreads>narray_max_threads) nthreads = narray_max_threads; + threads.resize(nthreads); + for(J=0;J, + this, + &other, + &ret, + J,nthreads + ); + } + for(J=0;Jjoin(); + delete threads[J]; + threads[J]= NULL; + } + } + } + return ret; + } + + template void narray_operator_div_tf( + const narray *a, + const narray *b, + narray *out, + int threadnum, int nthreads + ) + { + int I0,I1,Is,I; + + Is = a->length/nthreads; + I0 = Is*threadnum; + I1 = (threadnum>=(nthreads-1))? a->length: Is*(threadnum+1); + for(I=I0;Idata[I] = a->data[I] / b->data[I]; + } + + return; + } + + template narray narray::operator/(const narray& other) const + { + narray ret; + narray_size_t I; + int J; + int nthreads; + std::vector threads; + + if(this->length!=other.length) + { + ret.resize(0); + printf("narray::operator/:: error - array sizes %ld and %ld are not the same.\n",(long)this->length,(long)other.length); + } + + ret.resize(this->length); + + if(this->lengthlength;I++) ret.data[I] = this->data[I] / other.data[I]; + } + else + { + //threaded operation + nthreads = std::thread::hardware_concurrency(); + nthreads = (nthreads<1) ? 1 : nthreads; + nthreads = (nthreads>narray_max_threads) ? narray_max_threads : nthreads; + //if(nthreads<1) nthreads=1; + //if(nthreads>narray_max_threads) nthreads = narray_max_threads; + threads.resize(nthreads); + for(J=0;J, + this, + &other, + &ret, + J,nthreads + ); + } + for(J=0;Jjoin(); + delete threads[J]; + threads[J]= NULL; + } + } + } + return ret; + } + + template void narray_operator_add2_tf( + const narray *a, + const T val, + narray *out, + int threadnum, int nthreads + ) + { + int I0,I1,Is,I; + + Is = a->length/nthreads; + I0 = Is*threadnum; + I1 = (threadnum>=(nthreads-1))? a->length: Is*(threadnum+1); + for(I=I0;Idata[I] = a->data[I] + val; + } + + return; + } + + template narray narray::operator+(const T val) const + { + narray ret; + narray_size_t I; + int J; + int nthreads; + std::vector threads; + + ret.resize(this->length); + + if(this->lengthlength;I++) ret.data[I] = this->data[I] + val; + } + else + { + //threaded operation + nthreads = std::thread::hardware_concurrency(); + nthreads = (nthreads<1) ? 1 : nthreads; + nthreads = (nthreads>narray_max_threads) ? narray_max_threads : nthreads; + //if(nthreads<1) nthreads=1; + //if(nthreads>narray_max_threads) nthreads = narray_max_threads; + threads.resize(nthreads); + for(J=0;J, + this, + val, + &ret, + J,nthreads + ); + } + for(J=0;Jjoin(); + delete threads[J]; + threads[J]= NULL; + } + } + } + return ret; + } + + template void narray_operator_sub2_tf( + const narray *a, + const T val, + narray *out, + int threadnum, int nthreads + ) + { + int I0,I1,Is,I; + + Is = a->length/nthreads; + I0 = Is*threadnum; + I1 = (threadnum>=(nthreads-1))? a->length: Is*(threadnum+1); + for(I=I0;Idata[I] = a->data[I] - val; + } + + return; + } + + template narray narray::operator-(const T val) const + { + narray ret; + narray_size_t I; + int J; + int nthreads; + std::vector threads; + + ret.resize(this->length); + + if(this->lengthlength;I++) ret.data[I] = this->data[I] - val; + } + else + { + //threaded operation + nthreads = std::thread::hardware_concurrency(); + nthreads = (nthreads<1) ? 1 : nthreads; + nthreads = (nthreads>narray_max_threads) ? narray_max_threads : nthreads; + //if(nthreads<1) nthreads=1; + //if(nthreads>narray_max_threads) nthreads = narray_max_threads; + threads.resize(nthreads); + for(J=0;J, + this, + val, + &ret, + J,nthreads + ); + } + for(J=0;Jjoin(); + delete threads[J]; + threads[J]= NULL; + } + } + } + return ret; + } + + template void narray_operator_mult2_tf( + const narray *a, + const T val, + narray *out, + int threadnum, int nthreads + ) + { + int I0,I1,Is,I; + + Is = a->length/nthreads; + I0 = Is*threadnum; + I1 = (threadnum>=(nthreads-1))? a->length: Is*(threadnum+1); + for(I=I0;Idata[I] = a->data[I] * val; + } + + return; + } + + template narray narray::operator*(const T val) const + { + narray ret; + narray_size_t I; + int J; + int nthreads; + std::vector threads; + + ret.resize(this->length); + + if(this->lengthlength;I++) ret.data[I] = this->data[I] * val; + } + else + { + //threaded operation + nthreads = std::thread::hardware_concurrency(); + nthreads = (nthreads<1) ? 1 : nthreads; + nthreads = (nthreads>narray_max_threads) ? narray_max_threads : nthreads; + //if(nthreads<1) nthreads=1; + //if(nthreads>narray_max_threads) nthreads = narray_max_threads; + threads.resize(nthreads); + for(J=0;J, + this, + val, + &ret, + J,nthreads + ); + } + for(J=0;Jjoin(); + delete threads[J]; + threads[J]= NULL; + } + } + } + return ret; + } + + template void narray_operator_div2_tf( + const narray *a, + const T val, + narray *out, + int threadnum, int nthreads + ) + { + int I0,I1,Is,I; + + Is = a->length/nthreads; + I0 = Is*threadnum; + I1 = (threadnum>=(nthreads-1))? a->length: Is*(threadnum+1); + for(I=I0;Idata[I] = a->data[I] / val; + } + + return; + } + + template narray narray::operator/(const T val) const + { + narray ret; + narray_size_t I; + int J; + int nthreads; + std::vector threads; + + ret.resize(this->length); + + if(this->lengthlength;I++) ret.data[I] = this->data[I] / val; + } + else + { + //threaded operation + nthreads = std::thread::hardware_concurrency(); + nthreads = (nthreads<1) ? 1 : nthreads; + nthreads = (nthreads>narray_max_threads) ? narray_max_threads : nthreads; + //if(nthreads<1) nthreads=1; + //if(nthreads>narray_max_threads) nthreads = narray_max_threads; + threads.resize(nthreads); + for(J=0;J, + this, + val, + &ret, + J,nthreads + ); + } + for(J=0;Jjoin(); + delete threads[J]; + threads[J]= NULL; + } + } + } + return ret; + } + + + template void narray_operator_negative_tf( + const narray *a, + narray *out, + int threadnum, int nthreads + ) + { + int I0,I1,Is,I; + + Is = a->length/nthreads; + I0 = Is*threadnum; + I1 = (threadnum>=(nthreads-1))? a->length: Is*(threadnum+1); + for(I=I0;Idata[I] = -a->data[I]; + } + + return; + } + + template narray operator-(const narray &n) + { + narray ret; + narray_size_t I; + int J; + int nthreads; + std::vector threads; + + ret.resize(n.length); + + if(n.lengthnarray_max_threads) ? narray_max_threads : nthreads; + //if(nthreads<1) nthreads=1; + //if(nthreads>narray_max_threads) nthreads = narray_max_threads; + threads.resize(nthreads); + for(J=0;J, + &n, + &ret, + J,nthreads + ); + } + for(J=0;Jjoin(); + delete threads[J]; + threads[J]= NULL; + } + } + } + return ret; + } + + template void narray_applyfn_tf( + const narray *a, + T (*fnptr)(T), + narray *out, + int threadnum, int nthreads + ) + { + int I0,I1,Is,I; + + Is = a->length/nthreads; + I0 = Is*threadnum; + I1 = (threadnum>=(nthreads-1))? a->length: Is*(threadnum+1); + for(I=I0;Idata[I] = fnptr(a->data[I]); + } + + return; + } + + template narray narray::applyfn(T (*fnptr)(T)) const + { + narray ret; + narray_size_t I; + int J; + int nthreads; + std::vector threads; + + ret.resize(this->length); + + if(this->lengthlength;I++) ret.data[I] = fnptr(this->data[I]); + } + else + { + //threaded operation + nthreads = std::thread::hardware_concurrency(); + nthreads = (nthreads<1) ? 1 : nthreads; + nthreads = (nthreads>narray_max_threads) ? narray_max_threads : nthreads; + //if(nthreads<1) nthreads=1; + //if(nthreads>narray_max_threads) nthreads = narray_max_threads; + threads.resize(nthreads); + for(J=0;J, + this, + fnptr, + &ret, + J,nthreads + ); + } + for(J=0;Jjoin(); + delete threads[J]; + threads[J]= NULL; + } + } + } + return ret; + } + + template void narray_min_tf( + const narray *in, + narray *out, + int threadnum, int nthreads + ) + { + int I0,I1,Is,I; + + Is = in->length/nthreads; + I0 = Is*threadnum; + I1 = (threadnum>=(nthreads-1))? in->length: Is*(threadnum+1); + for(I=I0;Idata[I]data[threadnum]) + out->data[threadnum] = in->data[I]; + } + + return; + } + + template T narray::min() const + { + T ret = T(); + narray minarr; + narray_size_t I; + int J; + int nthreads; + std::vector threads; + + if(this->length<=0) + { + return ret; + } + + if(this->lengthdata[0]; + for(I=0;Ilength;I++) + { + if(this->data[I]data[I]; + } + } + } + else + { + //threaded operation + + nthreads = std::thread::hardware_concurrency(); + nthreads = (nthreads<1) ? 1 : nthreads; + nthreads = (nthreads>narray_max_threads) ? narray_max_threads : nthreads; + //if(nthreads<1) nthreads=1; + //if(nthreads>narray_max_threads) nthreads = narray_max_threads; + threads.resize(nthreads); + minarr.resize(nthreads); + minarr.setall(this->data[0]); + for(J=0;J, + this, + &minarr, + J,nthreads + ); + } + for(J=0;Jjoin(); + delete threads[J]; + threads[J]= NULL; + } + } + ret = this->data[0]; + for(J=0;J void narray_max_tf( + const narray *in, + narray *out, + int threadnum, int nthreads + ) + { + int I0,I1,Is,I; + + Is = in->length/nthreads; + I0 = Is*threadnum; + I1 = (threadnum>=(nthreads-1))? in->length: Is*(threadnum+1); + for(I=I0;Idata[I]>out->data[threadnum]) + out->data[threadnum] = in->data[I]; + } + + return; + } + + template T narray::max() const + { + T ret = T(); + narray maxarr; + narray_size_t I; + int J; + int nthreads; + std::vector threads; + + if(this->length<=0) + { + return ret; + } + + if(this->lengthdata[0]; + for(I=0;Ilength;I++) + { + if(this->data[I]>ret) + { + ret = this->data[I]; + } + } + } + else + { + //threaded operation + + nthreads = std::thread::hardware_concurrency(); + nthreads = (nthreads<1) ? 1 : nthreads; + nthreads = (nthreads>narray_max_threads) ? narray_max_threads : nthreads; + //if(nthreads<1) nthreads=1; + //if(nthreads>narray_max_threads) nthreads = narray_max_threads; + threads.resize(nthreads); + maxarr.resize(nthreads); + maxarr.setall(this->data[0]); + for(J=0;J, + this, + &maxarr, + J,nthreads + ); + } + for(J=0;Jjoin(); + delete threads[J]; + threads[J]= NULL; + } + } + ret = this->data[0]; + for(J=0;Jret) + { + ret = maxarr[J]; + } + } + } + return ret; + } + +}; //end namespace narray +}; //end namespace ams + +#endif \ No newline at end of file diff --git a/include/amscppnarray/amscppnarray_implsubarray.hpp b/include/amscppnarray/amscppnarray_implsubarray.hpp new file mode 100644 index 0000000..adfce90 --- /dev/null +++ b/include/amscppnarray/amscppnarray_implsubarray.hpp @@ -0,0 +1,44 @@ +#ifndef __AMSCPPNARRAY_IMPLSUBARRAY_HPP__ +#define __AMSCPPNARRAY_IMPLSUBARRAY_HPP__ + +namespace ams +{ +namespace narray +{ + + template narray narray::subarray(const narray_size_t ind1, const narray_size_t ind2) const + { + int res; + narray ret; + T defval = T(); + narray_size_t I,J; + + int nl = ind2-ind1; + + if(nl<=0) + { + ret.resize(0); + return ret; + } + + ret.resize(nl); + for(I=0;I=this->length) + { + ret.data[I] = defval; + } + else + { + ret.data[I] = this->data[J]; + } + } + + return ret; + } + +}; +}; + +#endif \ No newline at end of file diff --git a/include/amscppnarray/amscppnarray_llbops.hpp b/include/amscppnarray/amscppnarray_llbops.hpp new file mode 100644 index 0000000..92cca79 --- /dev/null +++ b/include/amscppnarray/amscppnarray_llbops.hpp @@ -0,0 +1,23 @@ +#ifndef __AMSCPPNARRAY_LLBOPS_HPP__ +#define __AMSCPPNARRAY_LLBOPS_HPP__ + +//Low level threaded buffer operations + +namespace ams +{ +namespace narray +{ + + template void buffer_copycast(T1 *bufferto, const T2 *bufferfrom, const narray_size_t N); + + + template void buffer_set(T *buffer, const T val, const narray_size_t ind1, const narray_size_t ind2); + + + +}; +}; + +#include + +#endif \ No newline at end of file diff --git a/include/amscppnarray/amscppnarray_llbopsimpl.hpp b/include/amscppnarray/amscppnarray_llbopsimpl.hpp new file mode 100644 index 0000000..125dc49 --- /dev/null +++ b/include/amscppnarray/amscppnarray_llbopsimpl.hpp @@ -0,0 +1,166 @@ +#ifndef __AMSCPPNARRAY_LLBOPSIMPL_HPP__ +#define __AMSCPPNARRAY_LLBOPSIMPL_HPP__ + +namespace ams +{ +namespace narray +{ + +template void buffer_copycast_tf( + T1 *bufferto, + const T2 *bufferfrom, + const narray_size_t N, + int threadnum, int nthreads +) +{ + int I0,I1,Is,I; + + Is = N/nthreads; + I0 = Is*threadnum; + I1 = (threadnum>=(nthreads-1))? N: Is*(threadnum+1); + for(I=I0;I void buffer_copycast(T1 *bufferto, const T2 *bufferfrom, const narray_size_t N) +{ + narray_size_t I; + int J; + int nthreads; + std::vector threads; + + if(N<=0) return; + + if(Nnarray_max_threads) ? narray_max_threads : nthreads; + threads.resize(nthreads); + + for(J=0;J, + bufferto, + bufferfrom, + N, + J,nthreads + ); + if(threads[J]==NULL) + { + printf("buffer_copy_cast: warning, thread %d failed to launch.\n",J); + } + } + for(J=0;Jjoin(); + delete threads[J]; + threads[J]= NULL; + } + } + } + + return; +} + +template void buffer_set_tf( + T *bufferto, + const T val, + const narray_size_t ind1, + const narray_size_t ind2, + const narray_size_t N, + int threadnum, int nthreads +) +{ + int I0,I1,Is,I; + + Is = N/nthreads; + I0 = Is*threadnum; + I1 = (threadnum>=(nthreads-1))? N: Is*(threadnum+1); + + I0 = I0 + ind1; + I1 = I1 + ind1; + + for(I=I0;I void buffer_set(T *buffer, const T val, const narray_size_t ind1, const narray_size_t ind2) +{ + narray_size_t I; + int J; + int nthreads; + std::vector threads; + + narray_size_t N = ind2-ind1; + if(N<=0) return; + + if(Nnarray_max_threads) ? narray_max_threads : nthreads; + threads.resize(nthreads); + + for(J=0;J, + buffer, + val, + ind1,ind2,N, + J,nthreads + ); + if(threads[J]==NULL) + { + printf("buffer_copy_cast: warning, thread %d failed to launch.\n",J); + } + } + for(J=0;Jjoin(); + delete threads[J]; + threads[J]= NULL; + } + } + } + + return; +} + + +}; +}; + +#endif \ No newline at end of file diff --git a/include/amscppnarray/amscppnarray_math.hpp b/include/amscppnarray/amscppnarray_math.hpp new file mode 100644 index 0000000..816d213 --- /dev/null +++ b/include/amscppnarray/amscppnarray_math.hpp @@ -0,0 +1,29 @@ +#ifndef __AMSCPPNARRAY_MATH_HPP__ +#define __AMSCPPNARRAY_MATH_HPP__ + +namespace ams +{ +namespace narray +{ + +template T narray_max(const narray &q); +template T narray_min(const narray &q); +template double narray_mean(const narray &q); +template double narray_var(const narray &q); +template double narray_stdev(const narray &q); +template double narray_L2norm(const narray &q); + +template T narray_sum(const narray &q); + +template narray narray_abs(narray &q); + +void narray_testmath1(); +void narray_testmath2(); +void narray_testmath3(); + +}; //end namespaces +}; + +#include + +#endif \ No newline at end of file diff --git a/include/amscppnarray/amscppnarray_mathimpl.hpp b/include/amscppnarray/amscppnarray_mathimpl.hpp new file mode 100644 index 0000000..74ecc96 --- /dev/null +++ b/include/amscppnarray/amscppnarray_mathimpl.hpp @@ -0,0 +1,388 @@ +#ifndef __AMSCPPNARRAY_MATHIMPL_HPP__ +#define __AMSCPPNARRAY_MATHIMPL_HPP__ + +namespace ams +{ +namespace narray +{ + + template T narray_max(const narray &q) + { + return q.max(); + } + + template T narray_min(const narray &q) + { + return q.min(); + } + + template void narray_sum_tf( + const narray *in, + narray *sums, + int threadnum, + int nthreads + ) + { + int I0,I1,Is,I; + + Is = in->length/nthreads; + I0 = Is*threadnum; + I1 = (threadnum>=(nthreads-1))? in->length: Is*(threadnum+1); + for(I=I0;Idata[threadnum] = sums->data[threadnum] + in->data[I]; + } + } + + template T narray_sum(const narray &q) + { + T ret = T(); + narray sums; + narray_size_t I; + int J; + int nthreads; + std::vector threads; + + if(q.length<=0) + { + return ret; + } + + if(q.lengthnarray_max_threads) ? narray_max_threads : nthreads; + threads.resize(nthreads); + sums.resize(nthreads); + sums.setall(T()); + for(J=0;J, + &q, + &sums, + J,nthreads + ); + if(threads[J]==NULL) + { + printf("narray_sum: warning: thread %d failed to initialize.\n",J); + //handle errors + } + } + for(J=0;Jjoin(); + delete threads[J]; + threads[J]= NULL; + } + } + ret = T(); + for(J=0;J double narray_mean(const narray &q) + { + double ret = 0.0; + if(q.length>0) + { + T sum = narray_sum(q); + ret = (double)sum/((double)q.length); + } + return ret; + } + + template void narray_variance_tf( + const narray *in, + narray *sums, + double mean, + int threadnum, + int nthreads + ) + { + int I0,I1,Is,I; + double q2; + Is = in->length/nthreads; + I0 = Is*threadnum; + I1 = (threadnum>=(nthreads-1))? in->length: Is*(threadnum+1); + for(I=I0;Idata[I]-mean; + sums->data[threadnum] = sums->data[threadnum] + q2*q2/((double)in->length); + } + } + + template double narray_var(const narray &q) + { + double ret = 0.0; + narray sums; + narray_size_t I; + int J; + int nthreads; + std::vector threads; + + double mn = ams::narray::narray_mean(q); + double q2; + + if(q.length<=0) + { + return ret; + } + + if(q.lengthnarray_max_threads) ? narray_max_threads : nthreads; + threads.resize(nthreads); + sums.resize(nthreads); + sums.setall(T()); + for(J=0;J, + &q, + &sums, + mn, + J,nthreads + ); + if(threads[J]==NULL) + { + printf("narray_var: warning: thread %d failed to initialize.\n",J); + //handle errors + } + } + for(J=0;Jjoin(); + delete threads[J]; + threads[J]= NULL; + } + } + ret = T(); + for(J=0;J double narray_stdev(const narray &q) + { + double ret = 0.0; + if(q.length>0) + { + ret = narray_var(q); + ret = ::sqrt(ret); + } + return ret; + } + + template void narray_L2norm_tf( + const narray *in, + narray *sums, + int threadnum, + int nthreads + ) + { + int I0,I1,Is,I; + double q2; + Is = in->length/nthreads; + I0 = Is*threadnum; + I1 = (threadnum>=(nthreads-1))? in->length: Is*(threadnum+1); + for(I=I0;Idata[I]; + sums->data[threadnum] = sums->data[threadnum] + q2*q2; + } + } + + template double narray_L2norm(const narray &q) + { + double ret = 0.0; + narray sums; + narray_size_t I; + int J; + int nthreads; + std::vector threads; + double q2; + + if(q.length<=0) + { + return ret; + } + + if(q.lengthnarray_max_threads) ? narray_max_threads : nthreads; + threads.resize(nthreads); + sums.resize(nthreads); + sums.setall(T()); + for(J=0;J, + &q, + &sums, + J,nthreads + ); + if(threads[J]==NULL) + { + printf("narray_L2norm: warning: thread %d failed to initialize.\n",J); + //handle errors + } + } + for(J=0;Jjoin(); + delete threads[J]; + threads[J]= NULL; + } + } + ret = T(); + for(J=0;J void narray_abs_tf( + const narray *in, + narray *out, + int threadnum, + int nthreads + ) + { + int I0,I1,Is,I; + double q2; + Is = in->length/nthreads; + I0 = Is*threadnum; + I1 = (threadnum>=(nthreads-1))? in->length: Is*(threadnum+1); + for(I=I0;Idata[I] = (in->data[I]data[I] : in->data[I]; + } + } + + + template narray narray_abs(narray &q) + { + narray ret; + narray_size_t I; + int J; + int nthreads; + std::vector threads; + + if(q.length<=0) + { + ret.resize(0); + return ret; + } + + ret.resize(q.length); + if(ret.length!=q.length) + { + printf("narray_abs: error - could not allocate return array.\n"); + ret.resize(0); + return ret; + } + + if(q.lengthnarray_max_threads) ? narray_max_threads : nthreads; + threads.resize(nthreads); + for(J=0;J, + &q, + &ret, + J,nthreads + ); + if(threads[J]==NULL) + { + printf("narray_abs: warning: thread %d failed to initialize.\n",J); + //handle errors + } + } + for(J=0;Jjoin(); + delete threads[J]; + threads[J]= NULL; + } + } + } + + return ret; + } + + +}; //end namespaces +}; + +#endif \ No newline at end of file diff --git a/include/amscppnarray/amscppnarray_random.hpp b/include/amscppnarray/amscppnarray_random.hpp new file mode 100644 index 0000000..1faf5b6 --- /dev/null +++ b/include/amscppnarray/amscppnarray_random.hpp @@ -0,0 +1,79 @@ +#ifndef __AMSCPPNARRAY_RANDOM_HPP__ +#define __AMSCPPNARRAY_RANDOM_HPP__ + +namespace ams +{ +namespace narray +{ +namespace rand +{ + + //Random functions for numeric array class + + //////////////////////////////////////////////////////////////////////// + //Simple Deterministic Psuedorandom Number Generator (32 bit version) // + //////////////////////////////////////////////////////////////////////// + + typedef int32_t narray_rands_t; + + static const narray_rands_t dpr32_mod = ( ((narray_rands_t)1) << ((narray_rands_t)30) ) - (narray_rands_t)1; + static const narray_rands_t dpr32_mult1 = ( (narray_rands_t) 1201633 ); + static const narray_rands_t dpr32_add1 = ( (narray_rands_t) 293482 ); + + extern narray_rands_t dpr32_rseed; // global random number seed for numeric array class + + narray_rands_t dpr32_nextseed(narray_rands_t seed); + double dpr32_randd(narray_rands_t *seed = &(ams::narray::rand::dpr32_rseed)); + float dpr32_randf(narray_rands_t *seed = &(ams::narray::rand::dpr32_rseed)); + double dpr32_gaussian(narray_rands_t *seed = &(ams::narray::rand::dpr32_rseed)); + float dpr32_gaussianf(narray_rands_t *seed = &(ams::narray::rand::dpr32_rseed)); + int dpr32_randint(int low, int high, narray_rands_t *seed = &(ams::narray::rand::dpr32_rseed)); + + ams::vec2 dpr32_randuvec2(narray_rands_t *seed); + ams::vec3 dpr32_randuvec3(narray_rands_t *seed); + ams::vec4 dpr32_randuvec4(narray_rands_t *seed); + + ams::vec2f dpr32_randuvec2f(narray_rands_t *seed); + ams::vec3f dpr32_randuvec3f(narray_rands_t *seed); + ams::vec4f dpr32_randuvec4f(narray_rands_t *seed); + + //////////////////////////////////////////// + //Numeric array threaded random generators// + //////////////////////////////////////////// + + narray_rands_t get_rseed(); + void set_rseed(narray_rands_t seed); + void set_rseed_withtimer(); + + narray narray_rand(narray_size_t N, + narray_rands_t *rseed = &(ams::narray::rand::dpr32_rseed)); + narray narray_randf(narray_size_t N, + narray_rands_t *rseed = &(ams::narray::rand::dpr32_rseed)); + narray narray_randgauss(narray_size_t N, + narray_rands_t *rseed = &(ams::narray::rand::dpr32_rseed)); + narray narray_randgaussf(narray_size_t N, + narray_rands_t *rseed = &(ams::narray::rand::dpr32_rseed)); + + narray narray_randint(narray_size_t N, int low, int highexcl, + narray_rands_t *rseed = &(ams::narray::rand::dpr32_rseed)); + narray narray_randuvec2(narray_size_t N, + narray_rands_t *rseed = &(ams::narray::rand::dpr32_rseed)); + narray narray_randuvec3(narray_size_t N, + narray_rands_t *rseed = &(ams::narray::rand::dpr32_rseed)); + narray narray_randuvec4(narray_size_t N, + narray_rands_t *rseed = &(ams::narray::rand::dpr32_rseed)); + narray narray_randuvec2f(narray_size_t N, + narray_rands_t *rseed = &(ams::narray::rand::dpr32_rseed)); + narray narray_randuvec3f(narray_size_t N, + narray_rands_t *rseed = &(ams::narray::rand::dpr32_rseed)); + narray narray_randuvec4f(narray_size_t N, + narray_rands_t *rseed = &(ams::narray::rand::dpr32_rseed)); + + + void test_random1(); + +}; +}; +}; + +#endif \ No newline at end of file diff --git a/make_linux.py b/make_linux.py new file mode 100644 index 0000000..a77ed99 --- /dev/null +++ b/make_linux.py @@ -0,0 +1,22 @@ +#!/usr/bin/python3 + +import os,sys,math +from build.amsbuildlib4 import * + +if(len(sys.argv)>=2): + if(sys.argv[1]=="clean"): + obj_list = flist('./build_linux64',recurse=True,exts=['.o']) + for o in obj_list: + os.remove('{}'.format(o)) + exit() + +os.system('python3 ./build/make.linux64.lib.py') +os.system('python3 ./build/make.linux64.test.py') + +obj_list = flist('./src',recurse=True,exts=['.o']) +for o in obj_list: + os.remove('{}'.format(o)) + +os.chdir('./build_linux64') +callproc('./tests') +os.chdir('..') diff --git a/make_mingw.py b/make_mingw.py new file mode 100644 index 0000000..7557e18 --- /dev/null +++ b/make_mingw.py @@ -0,0 +1,28 @@ +#!/usr/bin/python3 +#!/usr/bin/python3 + +import os,sys,math +from build.amsbuildlib4 import * + +if(len(sys.argv)>=2): + if(sys.argv[1]=="clean"): + obj_list = flist('./build_mingw64',recurse=True,exts=['.o']) + for o in obj_list: + os.remove('{}'.format(o)) + exit() + +os.system('python3 ./build/make.mingw64.lib.py') +os.system('python3 ./build/make.mingw64.test.py') + +obj_list = flist('./src',recurse=True,exts=['.o']) +for o in obj_list: + os.remove('{}'.format(o)) + +if(sys.platform!="win32"): + os.chdir('./build_mingw64') + callproc('wine ./tests.exe') + os.chdir('..') +else: + os.chdir('./build_mingw64') + callproc('tests.exe') + os.chdir('..') diff --git a/make_msvc.py b/make_msvc.py new file mode 100644 index 0000000..64c47aa --- /dev/null +++ b/make_msvc.py @@ -0,0 +1,23 @@ +#!/usr/bin/python3 +#!/usr/bin/python3 + +import os,sys,math +from build.amsbuildlib4 import * + +if(len(sys.argv)>=2): + if(sys.argv[1]=="clean"): + obj_list = flist('./build_msvc64',recurse=True,exts=['.o','.obj']) + for o in obj_list: + os.remove('{}'.format(o)) + exit() + +os.system('python3 ./build/make.msvc64.lib.py') +os.system('python3 ./build/make.msvc64.test.py') + +obj_list = flist('./src',recurse=True,exts=['.o','.obj']) +for o in obj_list: + os.remove('{}'.format(o)) + +os.chdir('./build_msvc64') +callproc('tests.exe') +os.chdir('..') \ No newline at end of file diff --git a/notes/.placeholder b/notes/.placeholder new file mode 100644 index 0000000..e69de29 diff --git a/pull.sh b/pull.sh new file mode 100644 index 0000000..8eaf03d --- /dev/null +++ b/pull.sh @@ -0,0 +1,4 @@ +#!/usr/bin/bash + +scp aschinder@amssolarempire.com:workspace/projects/amscppnarray.tar.gz ../ +tar xzvf ../amscppnarray.tar.gz ./ diff --git a/scripts/__pycache__/amsbinarrayrw.cpython-310.pyc b/scripts/__pycache__/amsbinarrayrw.cpython-310.pyc new file mode 100644 index 0000000..22f40f1 Binary files /dev/null and b/scripts/__pycache__/amsbinarrayrw.cpython-310.pyc differ diff --git a/scripts/amsbinarrayrw.py b/scripts/amsbinarrayrw.py new file mode 100644 index 0000000..96b7850 --- /dev/null +++ b/scripts/amsbinarrayrw.py @@ -0,0 +1,81 @@ +#!/usr/bin/python3 +import os,sys,math +import numpy as np + +#Write double ndarray +def fwrite_dndarray(fp,array): + + array = np.array(array,dtype=np.float64,order='F') + + Ndim = np.int32(len(array.shape)) + shp = np.int32(array.shape) + + q = Ndim.tobytes() + fp.write(q) + if(Ndim>0): + q = shp.tobytes() + fp.write(q) + + q = array.tobytes() + fp.write(q) + + return + +#Read double ndarray +def fread_dndarray(fp): + + array = np.zeros((),dtype=np.float64,order='F') + + q = fp.read(4) + Ndim = np.frombuffer(q,dtype=np.int32,count=1)[0] + + if(Ndim>0): + q = fp.read(4*Ndim) + shp = np.frombuffer(q,dtype=np.int32,count=Ndim) + piprod = 1 + for i in range(0,len(shp)): + piprod = piprod*shp[i] + q = fp.read(8*piprod) + array = np.frombuffer(q,dtype=np.float64,count=piprod) + array = array.reshape(shp,order='F') + + return array + +#Write float ndarray +def fwrite_fndarray(fp,array): + + array = np.array(array,dtype=np.float32,order='F') + + Ndim = np.int32(len(array.shape)) + shp = np.int32(array.shape) + + q = Ndim.tobytes() + fp.write(q) + if(Ndim>0): + q = shp.tobytes() + fp.write(q) + + q = array.tobytes() + fp.write(q) + + return + +#Read float ndarray +def fread_fndarray(fp): + + array = np.zeros((),dtype=np.float32,order='F') + + q = fp.read(4) + Ndim = np.frombuffer(q,dtype=np.int32,count=1)[0] + + if(Ndim>0): + q = fp.read(4*Ndim) + shp = np.frombuffer(q,dtype=np.int32,count=Ndim) + piprod = 1 + for i in range(0,len(shp)): + piprod = piprod*shp[i] + q = fp.read(4*piprod) + array = np.frombuffer(q,dtype=np.float32,count=piprod) + array = array.reshape(shp,order='F') + + return array \ No newline at end of file diff --git a/scripts/checkrandomness.py b/scripts/checkrandomness.py new file mode 100644 index 0000000..0c99dfa --- /dev/null +++ b/scripts/checkrandomness.py @@ -0,0 +1,31 @@ +#!/usr/bin/bash + +import os,sys,math +import numpy as np +import matplotlib.pyplot as plt + +from amsbinarrayrw import * + +def plot_random1(): + + fp = open("./build_linux64/tmp_testrand.bin","r+b") + data = fread_dndarray(fp) + N1 = data.shape[0] + N2 = int(math.sqrt(N1)) + N3 = N2*N2 + data = data[0:N3] + data = data.reshape([N2,N2]) + q = np.zeros([N2,N2,3]) + q[:,:,0] = data[:,:] + q[:,:,1] = data[:,:] + q[:,:,2] = data[:,:] + + hf = plt.figure(1) + plt.imshow(q) + plt.show() + fp.close() + +if(__name__=="__main__"): + plot_random1() + + pass \ No newline at end of file diff --git a/src/amscppnarray/amscppnarray.cpp b/src/amscppnarray/amscppnarray.cpp new file mode 100644 index 0000000..70ee5b3 --- /dev/null +++ b/src/amscppnarray/amscppnarray.cpp @@ -0,0 +1,230 @@ +#include + +namespace ams +{ +namespace narray +{ + +template<> void narray::print(const int printstyle) +{ + narray_size_t I; + + printf("{"); + for(I=0;Ilength-1;I++) + { + printf("%1.3f,",this->data[I]); + } + if(this->length>=1) printf("%1.3f",this->data[this->length-1]); + printf("}"); + + return; +} + +template<> void narray::print(const int printstyle) +{ + narray_size_t I; + + printf("{"); + for(I=0;Ilength-1;I++) + { + printf("%1.3f,",this->data[I]); + } + if(this->length>=1) printf("%1.3f",this->data[this->length-1]); + printf("}"); + + return; +} + +template<> void narray::print(const int printstyle) +{ + narray_size_t I; + + printf("{"); + for(I=0;Ilength-1;I++) + { + printf("%d,",this->data[I]); + } + if(this->length>=1) printf("%d",this->data[this->length-1]); + printf("}"); + + return; +} + +template<> void narray::print(const int printstyle) +{ + narray_size_t I; + + printf("{"); + for(I=0;Ilength-1;I++) + { + printf("%ld,",this->data[I]); + } + if(this->length>=1) printf("%ld",this->data[this->length-1]); + printf("}"); + + return; +} + +void test_narray1() +{ + int I; + narray a; + narray *b = NULL; + narray tmp; + + b = new(std::nothrow) narray(); + a.resize(100); + b->resize(100000); + a.setall(2.5f); + b->setall(3.0f); + + for(I=0;I<100000;I++) + { + if(b->at(I)!=3.0f) + { + printf("Error: b[%d]=%1.3f, not %1.3f\n",I,b->at(I),3.0f); + break; + } + } + + a.resize(100000); + + tmp = a; + a = *b; + *b = tmp; + + *b = tmp+a; + for(I=0;Ilength;I++) + { + if(b->at(I) != tmp[I] + a[I]) + { + printf("addition error in index %d",I); + } + } + + printf("threaded boolean test: %d\n", (*b==(a+tmp))); + + + + delete b; + + return; +} + +void test_narray2() +{ + int I; + narray a,b,c,d; + + a.resize(100000); a.setall(1.0f); + b.resize(100000); for(I=0;I a,b,c,d; + + a.resize(5); a.setall(1.0f); + b = {0,0.25,0.5,0.675,0.8675}; + c.resize(5); c.setall(2.0f); + + d = -a*(b+c*2+2); + for(I=0;I<5;I++) + { + printf("d[%d]=%1.3f\n",I,d[I]); + + } + + printf("d==-a*(b+c*2+2):%d\n",d==(-a*(b+c*2+2))); + d[4] = 0.0f; + printf("d==-a*(b+c*2+2):%d\n",d==(-a*(b+c*2+2))); + d = d.applyfn(::sinf); + + for(I=0;I<5;I++) + { + printf("d[%d]=%1.3f\n",I,d[I]); + + } + + d = d.subarray(0,1); + for(I=0;I q; + int I,J,K; + + q.insert(0,1); + q.print(); printf("\n"); + q.insert(0,2); + q.print(); printf("\n"); + q.insert(0,3); + q.print(); printf("\n"); + q.insert(-1,1); + q.print(); printf("\n"); + q.insert(10,99); + q.print(); printf("\n"); + + I = 3; + printf("q.find(%d)=%d\n",I,(int)q.find(I)); + I = 1; + printf("q.find(%d)=%d\n",I,(int)q.find(I)); + I = 5; + printf("q.find(%d)=%d\n",I,(int)q.find(I)); + I = 99; + printf("q.find(%d)=%d\n",I,(int)q.find(I)); + + I = 1; + printf("q.findinsert(%d)=%d\n",I,(int)q.find_insert_ordered(I)); + I = -1; + printf("q.findinsert(%d)=%d\n",I,(int)q.find_insert_ordered(I)); + I = 5; + printf("q.findinsert(%d)=%d\n",I,(int)q.find_insert_ordered(I)); + I = 99; + printf("q.findinsert(%d)=%d\n",I,(int)q.find_insert_ordered(I)); + I = 100; + printf("q.findinsert(%d)=%d\n",I,(int)q.find_insert_ordered(I)); + + q.erase(0); + q.print(); printf("\n"); + q.erase(-1); + q.print(); printf("\n"); + q.erase(q.length); + q.print(); printf("\n"); + q.erase(q.length-1); + q.print(); printf("\n"); + + q.resize(0); + for(I=0;I<25;I++) + { + J = rand::dpr32_randint(5,25); + K = q.find_insert_ordered(J); + q.insert(K,J); + q.print(); printf("\n"); + } + + return; +} + +}; //end namespace narray +}; //end namespace ams \ No newline at end of file diff --git a/src/amscppnarray/amscppnarray_inits.cpp b/src/amscppnarray/amscppnarray_inits.cpp new file mode 100644 index 0000000..a354afb --- /dev/null +++ b/src/amscppnarray/amscppnarray_inits.cpp @@ -0,0 +1,173 @@ +#include + +namespace ams +{ +namespace narray +{ + +void narray_linspacef_tf( + narray *in, + const float low, const float high, const narray_size_t N, + int threadnum, int nthreads +) +{ + int I0,I1,Is,I; + float g; + + Is = in->length/nthreads; + I0 = Is*threadnum; + I1 = (threadnum>=(nthreads-1))? in->length: Is*(threadnum+1); + for(I=I0;Idata[I] = low*(1.0f-g) + high*g; + } +} + + +narray linspacef(const float low, const float high, narray_size_t N) +{ + narray ret; + narray_size_t I; + int J; + int nthreads; + std::vector threads; + float g; + + if(N<=0) + { + N = 0; + } + ret.resize(N); + + if(ret.length==0) + { + return ret; + } + + if(ret.lengthnarray_max_threads) ? narray_max_threads : nthreads; + //if(nthreads<1) nthreads=1; + //if(nthreads>narray_max_threads) nthreads = narray_max_threads; + threads.resize(nthreads); + for(J=0;Jjoin(); + delete threads[J]; + threads[J]= NULL; + } + } + } + + + + return ret; +} + +void narray_linspace_tf( + narray *in, + const double low, const double high, const narray_size_t N, + int threadnum, int nthreads +) +{ + int I0,I1,Is,I; + double g; + + Is = in->length/nthreads; + I0 = Is*threadnum; + I1 = (threadnum>=(nthreads-1))? in->length: Is*(threadnum+1); + for(I=I0;Idata[I] = low*(1.0-g) + high*g; + } +} + +narray linspace(const double low, const double high, narray_size_t N) +{ + narray ret; + narray_size_t I; + int J; + int nthreads; + std::vector threads; + double g; + + if(N<=0) + { + N = 0; + } + ret.resize(N); + + if(ret.length==0) + { + return ret; + } + + if(ret.lengthnarray_max_threads) ? narray_max_threads : nthreads; + //if(nthreads<1) nthreads=1; + //if(nthreads>narray_max_threads) nthreads = narray_max_threads; + threads.resize(nthreads); + for(J=0;Jjoin(); + delete threads[J]; + threads[J]= NULL; + } + } + } + + + + return ret; +} + + +}; //end namespaces +}; \ No newline at end of file diff --git a/src/amscppnarray/amscppnarray_math.cpp b/src/amscppnarray/amscppnarray_math.cpp new file mode 100644 index 0000000..3115af7 --- /dev/null +++ b/src/amscppnarray/amscppnarray_math.cpp @@ -0,0 +1,110 @@ +#include + +namespace ams +{ +namespace narray +{ + + static void _intl_printarr(narray &q) + { + int I; + if(q.length>0) + { + printf("{"); + for(I=0;I &q) + { + int I; + if(q.length>0) + { + printf("{"); + for(I=0;I a = {-4,-3,-2,-1,0,1,2,3,4,5}; + narray b; + int asum = narray_sum(a); + double amean = narray_mean(a); + + printf("sum(a)=%d\n",asum); + printf("mean(a)=%1.3f\n",amean); + printf("stdev(a)=%1.3f\n",narray_stdev(a)); + printf("L2norm(a)=%1.3f\n",narray_L2norm(a)); + printf("min(a)=%d\n",narray_min(a)); + printf("max(a)=%d\n",narray_max(a)); + + b = narray_abs(a); + _intl_printarr(b); printf("\n"); + + return; + } + + void narray_testmath2() + { + //rand::narray_rands_t rs = 1943981; + rand::set_rseed_withtimer(); + printf("rseed: %d\n",(int)rand::get_rseed()); + printf("rseed: %d\n",(int)rand::dpr32_rseed); //< -- this is different than the line above + + + narray unif = ams::narray::rand::narray_rand(10000*10000); + + + printf("rseed: %d\n",(int)rand::get_rseed()); + + double mn1 = narray_mean(unif); + double std1 = narray_stdev(unif); + double mn2,std2; + + + + //_intl_printarr(unif); printf("\n"); + + unif = unif*2.0-1.0; + //unif.setall(3.5); + //_intl_printarr(unif); printf("\n"); + mn2 = narray_mean(unif); + std2 = narray_stdev(unif); + + + + + printf("mn1=%1.3f std1=%1.3f\n",mn1,std1); + printf("mn2=%1.3f std2=%1.3f\n",mn2,std2); + + + return; + } + + void narray_testmath3() + { + narray rb; + const char *fout = "./tmp_testrand.bin"; + FILE *fp = NULL; + fp = fopen(fout,"w+b"); + + rb = rand::narray_rand(500*500); + fwrite_ndarray(fp,&rb,NULL); + + fclose(fp); + + } + +}; +}; \ No newline at end of file diff --git a/src/amscppnarray/amscppnarray_random.cpp b/src/amscppnarray/amscppnarray_random.cpp new file mode 100644 index 0000000..dca9d32 --- /dev/null +++ b/src/amscppnarray/amscppnarray_random.cpp @@ -0,0 +1,592 @@ +#include + +namespace ams +{ +namespace narray +{ +namespace rand +{ + + //defining global random number seed for numeric array random routines + narray_rands_t dpr32_rseed = 0; + + narray_rands_t dpr32_nextseed(narray_rands_t seed) + { + narray_rands_t sret = seed; + sret = ams::mod(sret*dpr32_mult1+dpr32_add1,dpr32_mod); + return sret; + } + + double dpr32_randd(narray_rands_t *seed) + { + double ret; + //printf("debug: seed: %d\n",(int)*seed); + *seed = ams::narray::rand::dpr32_nextseed(*seed); + //printf("debug: seed: %d\n",(int)*seed); + ret = (double)*seed/(double)(dpr32_mod-1); + return ret; + } + + float dpr32_randf(narray_rands_t *seed) + { + float ret; + *seed = ams::narray::rand::dpr32_nextseed(*seed); + ret = (float)*seed/(float)(dpr32_mod-1); + return ret; + } + + double dpr32_gaussian(narray_rands_t *seed) + { + double ret = 0.0; + double u1,u2; + u1 = ams::narray::rand::dpr32_randd(seed); + u2 = ams::narray::rand::dpr32_randd(seed); + + if(u1>0.0) + { + ret = ::sqrt(-2.0*::log(u1))*::cos(2.0*pi*u2); + } + + return ret; + } + + + float dpr32_gaussianf(narray_rands_t *seed) + { + float ret = 0.0f; + float u1,u2; + u1 = ams::narray::rand::dpr32_randf(seed); + u2 = ams::narray::rand::dpr32_randf(seed); + + if(u1>0.0) + { + ret = ::sqrt(-2.0f*::log(u1))*::cos(2.0f*pif*u2); + } + + return ret; + } + + vec2 dpr32_randuvec2(narray_rands_t *seed) + { + vec2 ret = vec2(0,0); + double th = ams::narray::rand::dpr32_randd(seed)*2.0*pi; + ret = vec2(cos(th),sin(th)); + return ret; + } + + vec3 dpr32_randuvec3(narray_rands_t *seed) + { + vec3 ret; + double az,el; + el = ::acos(2.0*dpr32_randd(seed)-1.0)-pi/2.0; + az = ams::narray::rand::dpr32_randd(seed)*2.0*pi; + + ret = vec3(cos(az)*cos(el),sin(az)*cos(el),sin(el)); + + return ret; + } + + vec4 dpr32_randuvec4(narray_rands_t *seed) + { + vec4 ret; + double x,y,z,w; + + x = ams::narray::rand::dpr32_gaussian(seed); + y = ams::narray::rand::dpr32_gaussian(seed); + z = ams::narray::rand::dpr32_gaussian(seed); + w = ams::narray::rand::dpr32_gaussian(seed); + ret = vec4(x,y,z,w); + ret = vnormalize(ret); + + return ret; + } + + vec2f dpr32_randuvec2f(narray_rands_t *seed) + { + vec2f ret = vec2f(0,0); + float th = ams::narray::rand::dpr32_randf(seed)*2.0*pi; + ret = vec2f(cos(th),sin(th)); + return ret; + } + + vec3f dpr32_randuvec3f(narray_rands_t *seed) + { + vec3f ret; + float az,el; + el = ::acos(2.0f*dpr32_randf(seed)-1.0)-pif/2.0; + az = ams::narray::rand::dpr32_randf(seed)*2.0f*pif; + + ret = vec3f(cos(az)*cos(el),sin(az)*cos(el),sin(el)); + + return ret; + } + + vec4f dpr32_randuvec4f(narray_rands_t *seed) + { + vec4f ret; + float x,y,z,w; + + x = ams::narray::rand::dpr32_gaussianf(seed); + y = ams::narray::rand::dpr32_gaussianf(seed); + z = ams::narray::rand::dpr32_gaussianf(seed); + w = ams::narray::rand::dpr32_gaussianf(seed); + ret = vec4f(x,y,z,w); + ret = vnormalize(ret); + + return ret; + } + + narray_rands_t get_rseed() + { + return ams::narray::rand::dpr32_rseed; + } + + void set_rseed(narray_rands_t seed) + { + ams::narray::rand::dpr32_rseed = seed; + } + + void set_rseed_withtimer() + { + narray_rands_t t1 = (narray_rands_t)time(NULL); + narray_rands_t t2 = (narray_rands_t)(::fmod((double)clock()/((double)CLOCKS_PER_SEC)*1000.0f,36000.0f)); + ams::narray::rand::dpr32_rseed = (narray_rands_t)t1 + (narray_rands_t)t2; + } + + template void narray_rand_threadf1( + narray *out, + narray *rseeds, + T (*randfunc)(narray_rands_t *), + int threadnum, + int nthreads + ) + { + int I0,I1,Is,I; + Is = out->length/nthreads; + I0 = Is*threadnum; + I1 = (threadnum>=(nthreads-1)) ? out->length : Is*(threadnum+1); + for(I=I0;Idata[I] = randfunc(&(rseeds->data[threadnum])); + } + return; + } + + template void narray_random_threadexec1( + narray *out, + narray_size_t N, + T (*randfunc)(narray_rands_t *), + narray_rands_t *rseed + ) + { + narray_size_t I; + int J; + int nthreads; + std::vector threads; + narray rseeds; + int res; + + res = out->resize(N); + if(res!=narray_success) + { + out->resize(0); + return; + } + + if(Ndata[I] = randfunc(rseed); + } + } + else + { + //multi-threaded operation + nthreads = std::thread::hardware_concurrency(); + nthreads = (nthreads<1) ? 1 : nthreads; + nthreads = (nthreads>narray_max_threads) ? narray_max_threads : nthreads; + threads.resize(nthreads); + rseeds.resize(nthreads); + + for(J=0;J, + out, + &rseeds, + randfunc, + J,nthreads + ); + if(threads[J]==NULL) + { + //handle thread allocation error + printf("warning: narray_random_threadexec1:: thread %d failed to allocate.\n",J); + } + } + for(J=0;Jjoin(); + delete threads[J]; + threads[J]= NULL; + } + } + } + + return; + } + + narray narray_rand(narray_size_t N, + narray_rands_t *rseed) + { + narray ret; + + if(N<=0) + { + ret.resize(0); + return ret; + } + + narray_random_threadexec1( + &ret, + N, + &(ams::narray::rand::dpr32_randd), + rseed + ); + + return ret; + } + + narray narray_randf(narray_size_t N, + narray_rands_t *rseed) + { + narray ret; + + if(N<=0) + { + ret.resize(0); + return ret; + } + + narray_random_threadexec1( + &ret, + N, + &(ams::narray::rand::dpr32_randf), + rseed + ); + + return ret; + } + + narray narray_randgauss(narray_size_t N, + narray_rands_t *rseed) + { + narray ret; + + if(N<=0) + { + ret.resize(0); + return ret; + } + + narray_random_threadexec1( + &ret, + N, + &(ams::narray::rand::dpr32_gaussian), + rseed + ); + + return ret; + } + + narray narray_randgaussf(narray_size_t N, + narray_rands_t *rseed) + { + narray ret; + + if(N<=0) + { + ret.resize(0); + return ret; + } + + narray_random_threadexec1( + &ret, + N, + &(ams::narray::rand::dpr32_gaussianf), + rseed + ); + + return ret; + } + + narray narray_randuvec2(narray_size_t N, + narray_rands_t *rseed) + { + narray ret; + + if(N<=0) + { + ret.resize(0); + return ret; + } + + narray_random_threadexec1( + &ret, + N, + &(ams::narray::rand::dpr32_randuvec2), + rseed + ); + + return ret; + } + + narray narray_randuvec3(narray_size_t N, + narray_rands_t *rseed) + { + narray ret; + + if(N<=0) + { + ret.resize(0); + return ret; + } + + narray_random_threadexec1( + &ret, + N, + &(ams::narray::rand::dpr32_randuvec3), + rseed + ); + + return ret; + } + + narray narray_randuvec4(narray_size_t N, + narray_rands_t *rseed) + { + narray ret; + + if(N<=0) + { + ret.resize(0); + return ret; + } + + narray_random_threadexec1( + &ret, + N, + &(ams::narray::rand::dpr32_randuvec4), + rseed + ); + + return ret; + } + + narray narray_randuvec2f(narray_size_t N, + narray_rands_t *rseed) + { + narray ret; + + if(N<=0) + { + ret.resize(0); + return ret; + } + + narray_random_threadexec1( + &ret, + N, + &(ams::narray::rand::dpr32_randuvec2f), + rseed + ); + + return ret; + } + + narray narray_randuvec3f(narray_size_t N, + narray_rands_t *rseed) + { + narray ret; + + if(N<=0) + { + ret.resize(0); + return ret; + } + + narray_random_threadexec1( + &ret, + N, + &(ams::narray::rand::dpr32_randuvec3f), + rseed + ); + + return ret; + } + + narray narray_randuvec4f(narray_size_t N, + narray_rands_t *rseed) + { + narray ret; + + if(N<=0) + { + ret.resize(0); + return ret; + } + + narray_random_threadexec1( + &ret, + N, + &(ams::narray::rand::dpr32_randuvec4f), + rseed + ); + + return ret; + } + + + int dpr32_randint(int low, int high, narray_rands_t *seed) + { + int ret = 0; + + + if(high-low>0) + { + *seed = dpr32_nextseed(*seed); + ret = low + (int)((*seed)%(high-low)); + } + + return ret; + } + + template void narray_rand_threadf2( + narray *out, + narray *rseeds, + T (*randfunc)(T, T, narray_rands_t*), + T low, + T highexcl, + int threadnum, + int nthreads + ) + { + int I0,I1,Is,I; + Is = out->length/nthreads; + I0 = Is*threadnum; + I1 = (threadnum>=(nthreads-1)) ? out->length : Is*(threadnum+1); + for(I=I0;Idata[I] = randfunc(low,highexcl,&(rseeds->data[threadnum])); + } + return; + } + + template void narray_random_threadexec2( + narray *out, + narray_size_t N, + T (*randfunc)(T, T, narray_rands_t *), + T low, + T highexcl, + narray_rands_t *rseed + ) + { + narray_size_t I; + int J; + int nthreads; + std::vector threads; + narray rseeds; + int res; + + res = out->resize(N); + if(res!=narray_success) + { + out->resize(0); + return; + } + + if(Ndata[I] = randfunc(low,highexcl,rseed); + } + } + else + { + //multi-threaded operation + nthreads = std::thread::hardware_concurrency(); + nthreads = (nthreads<1) ? 1 : nthreads; + nthreads = (nthreads>narray_max_threads) ? narray_max_threads : nthreads; + threads.resize(nthreads); + rseeds.resize(nthreads); + for(J=0;J, + out, + &rseeds, + randfunc, + low,highexcl, + J,nthreads + ); + if(threads[J]==NULL) + { + //handle thread allocation error + printf("warning: narray_random_threadexec1:: thread %d failed to allocate.\n",J); + } + } + for(J=0;Jjoin(); + delete threads[J]; + threads[J]= NULL; + } + } + } + + return; + } + + narray narray_randint(narray_size_t N, int low, int highexcl, + narray_rands_t *rseed) + { + narray ret; + + if(N<=0) + { + ret.resize(0); + return ret; + } + + narray_random_threadexec2( + &ret, + N, + &(ams::narray::rand::dpr32_randint), + low,highexcl, + rseed + ); + + return ret; + } + + +}; //end namespace rand +}; //end namespaces +}; \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..0dc4ecc --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,16 @@ +#include + +int main(int argc, char* argv[]) +{ + int ret = 0; + printf("ams c++ numeric array library tests.\n"); + ams::narray::test_narray1(); + ams::narray::test_narray2(); + ams::narray::test_narray3(); + ams::narray::narray_testmath1(); + ams::narray::narray_testmath2(); + ams::narray::narray_testmath3(); + ams::narray::test_insertdelete1(); + + return ret; +} \ No newline at end of file