Skip to content

Commit bb95535

Browse files
committed
formatting
1 parent 2e19d8e commit bb95535

File tree

9 files changed

+85
-11
lines changed

9 files changed

+85
-11
lines changed

benchrunner/Benchrunner.hs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,12 @@ dobench bench parorseq mb_size iters = do
218218
unless (isSorted (res0)) (error $ show bench ++ ": result not sorted.")
219219
putStrLn "Sorted: OK"
220220
pure (length arr, length res0, tmed0, tall0)
221+
CxxSort alg -> do
222+
arr <- getInputAsList alg mb_size
223+
(res0, tmed0, tall0) <- M.benchAndRunCxxSorts alg arr iters
224+
unless (isSorted (res0)) (error $ show bench ++ ": result not sorted.")
225+
putStrLn "Sorted: OK"
226+
pure (length arr, length res0, tmed0, tall0)
221227
_ -> error "dobench: case not implemented!"
222228

223229
{-

benchrunner/ForeignFunctionImports.hs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module ForeignFunctionImports (c_insertionsort, c_mergesort, c_quicksort, SortFn) where
1+
module ForeignFunctionImports (c_insertionsort, c_mergesort, c_quicksort, cxx_int_insertionsort, cxx_int_mergesort, cxx_int_quicksort, SortFn, SortFnCxx) where
22

33
import Foreign as F
44
import Foreign.C.Types as CTypes
@@ -9,3 +9,8 @@ type SortFn = Ptr Int64 -> CTypes.CSize -> CTypes.CSize -> IO (Ptr Int64)
99
foreign import ccall "insertionsort_inplace" c_insertionsort :: SortFn
1010
foreign import ccall "smergesort" c_mergesort :: SortFn
1111
foreign import ccall "quicksort_inplace" c_quicksort :: SortFn
12+
13+
type SortFnCxx = Ptr Int64 -> CTypes.CSize -> IO (Ptr Int64)
14+
foreign import ccall "insertionsort_cxx_int" cxx_int_insertionsort :: SortFnCxx
15+
foreign import ccall "bottom_up_merge_sort_cxx_int" cxx_int_mergesort :: SortFnCxx
16+
foreign import ccall "quicksort_cxx_int" cxx_int_quicksort :: SortFnCxx

benchrunner/Measure.hs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module Measure (benchAndRunCSorts, benchAndRunDataVecSorts, benchOnArrays, bench, benchPar, dotrialIO, benchIO, benchParIO) where
1+
module Measure (benchAndRunCSorts, benchAndRunCxxSorts, benchAndRunDataVecSorts, benchOnArrays, bench, benchPar, dotrialIO, benchIO, benchParIO) where
22

33
import Control.Exception (evaluate)
44
import Control.Monad.Par hiding (runParIO)
@@ -157,6 +157,13 @@ sortFnC alg = case alg of
157157
Quicksort -> FFI.c_quicksort
158158
_ -> error "sortFnC: Csort not implemented!"
159159

160+
sortFnCxx :: SortAlgo -> FFI.SortFnCxx
161+
sortFnCxx alg = case alg of
162+
Insertionsort -> FFI.cxx_int_insertionsort
163+
Mergesort -> FFI.cxx_int_mergesort
164+
Quicksort -> FFI.cxx_int_quicksort
165+
_ -> error "sortFnCxx: Csort not implemented!"
166+
160167
-- return type : IO ([Int64], Double, Double)
161168
-- [Int64]: sorted output array from the last iteration that was run
162169
-- Double: median runtime from the iterations that were run (selftimed)
@@ -184,3 +191,32 @@ benchAndRunCSorts salg arr iters = do
184191
putStrLn ("iter time: " ++ show delt)
185192
!sortedArr <- peekArray arrLength (castPtr sortedPtr :: Ptr Int64)
186193
return $! (sortedArr, delt)
194+
195+
196+
-- return type : IO ([Int64], Double, Double)
197+
-- [Int64]: sorted output array from the last iteration that was run
198+
-- Double: median runtime from the iterations that were run (selftimed)
199+
-- Double: Total time taken to run all the iterations (batchtime)
200+
benchAndRunCxxSorts :: SortAlgo -> [Int64] -> Int -> IO ([Int64], Double, Double)
201+
benchAndRunCxxSorts salg arr iters = do
202+
!tups <- mapM (\_ -> do
203+
!ptr <- newArray arr
204+
res <- dotrialCxx salg (length arr) ptr
205+
pure res
206+
) [1..iters]
207+
let (results, times) = unzip tups
208+
-- print times
209+
let selftimed = median times
210+
batchtime = sum times
211+
return $! (last results, selftimed, batchtime)
212+
where
213+
dotrialCxx alg arrLength ptr = do
214+
performMajorGC
215+
let fn = sortFnCxx alg
216+
t1 <- getCurrentTime
217+
!sortedPtr <- fn ptr (fromIntegral arrLength)
218+
t2 <- getCurrentTime
219+
let delt = fromRational (toRational (diffUTCTime t2 t1))
220+
putStrLn ("iter time: " ++ show delt)
221+
!sortedArr <- peekArray arrLength (castPtr sortedPtr :: Ptr Int64)
222+
return $! (sortedArr, delt)

benchrunner/Types.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ data Benchmark
2323
| OurSort SortAlgo
2424
| VectorSort SortAlgo
2525
| CSort SortAlgo
26+
| CxxSort SortAlgo
2627
deriving (Eq, Show, Read)
2728

2829
data ParOrSeq = Seq | Par | ParM

benchrunner/benchrunner.cabal

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
cabal-version: >=1.10
2-
1+
cabal-version: 2.2
32
name: benchrunner
43
version: 0.1
54
build-type: Simple
@@ -33,14 +32,38 @@ executable benchrunner
3332
ghc-options: -Wall -Wcompat
3433
-threaded -rtsopts
3534
-fdefer-typed-holes
36-
-O2
35+
-O2 -optl -lstdc++ -optl -lgcc_s
36+
37+
38+
3739

3840
c-sources:
3941
csorts/insertionsort.c
4042
csorts/mergesort.c
4143
csorts/quicksort.c
4244

45+
cxx-sources:
46+
cxx/insertionsort.cpp
47+
cxx/mergesort.cpp
48+
cxx/quicksort.cpp
49+
cxx/insertionsort_int_wrapper.cpp
50+
cxx/mergesort_int_wrapper.cpp
51+
cxx/quicksort_int_wrapper.cpp
52+
53+
if impl(ghc >= 9.4)
54+
build-depends: system-cxx-std-lib
55+
else
56+
if os(windows)
57+
extra-libraries: stdc++
58+
if os(linux)
59+
extra-libraries: stdc++
60+
if os(darwin)
61+
extra-libraries: stdc++
62+
63+
4364
-- DNDEBUG disables asserts in cbits/
4465
cc-options: -std=c11 -O3 -DNDEBUG=1
4566
-fno-strict-aliasing
4667
-Werror=undef
68+
69+
cpp-options: -std=c++11 -O3

benchrunner/cxx/benchmarks.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ void *cilksort_par(void *const pbase, size_t total_elems, size_t size, __compar_
2929
template <typename T> T *bottomUpMergeSort(T *a, T *b, int n);
3030

3131
// Relating to C++ templatized versions
32-
int64_t *bottom_up_merge_sort_int(int64_t *pbase, size_t total_elems);
33-
int64_t *insertionsort_int(int64_t *pbase, size_t total_elems);
34-
int64_t *quicksort_int(int64_t *pbase, size_t total_elems);
32+
int64_t *bottom_up_merge_sort_cxx_int(int64_t *pbase, size_t total_elems);
33+
int64_t *insertionsort_cxx_int(int64_t *pbase, size_t total_elems);
34+
int64_t *quicksort_cxx_int(int64_t *pbase, size_t total_elems);
3535

3636
// Microbenchmarks.
3737
int64_t* __attribute__ ((noinline)) fill_array_seq(size_t total_elems, int64_t val);
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#include "benchmarks.h"
22
#include "insertionsort.cpp"
33

4-
int64_t *insertionsort_int(int64_t *pbase, size_t total_elems){
4+
5+
int64_t *insertionsort_cxx_int(int64_t *pbase, size_t total_elems);
6+
7+
int64_t *insertionsort_cxx_int(int64_t *pbase, size_t total_elems){
58
return insertionsort_inplace<int64_t>(pbase, total_elems);
69
}

benchrunner/cxx/mergesort_int_wrapper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#include "mergesort.cpp"
33

44

5-
int64_t *bottom_up_merge_sort_int(int64_t *pbase, size_t total_elems){
5+
int64_t *bottom_up_merge_sort_cxx_int(int64_t *pbase, size_t total_elems){
66

77
int64_t *copy_pbase = new int64_t[total_elems];
88
copyArray<int64_t>(pbase, copy_pbase, total_elems);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "benchmarks.h"
22
#include "quicksort.cpp"
33

4-
int64_t *quicksort_int(int64_t *pbase, size_t total_elems){
4+
int64_t *quicksort_cxx_int(int64_t *pbase, size_t total_elems){
55
return quicksort_inplace<int64_t>(pbase, total_elems);
66
}

0 commit comments

Comments
 (0)