-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsum.py
More file actions
38 lines (29 loc) · 960 Bytes
/
Copy pathsum.py
File metadata and controls
38 lines (29 loc) · 960 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import sys
import os
import ctypes
import argparse
import numpy as np
np.set_printoptions(precision=2)
def parse_args():
default_path = os.path.join("..", "libshared", "Debug", "libshared.dll")
parser = argparse.ArgumentParser(description='Sums a random numpy array.')
parser.add_argument('shape', type=int, nargs='+', help='Two terms to sum')
parser.add_argument('--dll', default=default_path)
args = parser.parse_args()
return args
args = parse_args()
path_to_dll = args.dll
shape = args.shape
dll = ctypes.cdll.LoadLibrary(path_to_dll)
double_pointer = ctypes.POINTER(ctypes.c_double)
dll.sum.argtypes = (double_pointer, ctypes.c_size_t)
dll.sum.restype = ctypes.c_double
array = np.random.randn(*shape)
array = array.astype(ctypes.c_double)
print(array)
pointer = array.ctypes.data_as(double_pointer)
nelem = array.size
s1 = dll.sum(pointer, array.size)
s2 = array.sum()
print(f"C++:\t{s1}")
print(f"numpy:\t{s2}")