"vscode:/vscode.git/clone" did not exist on "1ade42f72998ec47147cb35e10f5d2283737e420"
test_shared_mem_store.py 10.2 KB
Newer Older
1
2
3
4
5
""" NOTE(zihao) The unittest on shared memory store is temporally disabled because we 
have not fixed the bug described in https://github.com/dmlc/dgl/issues/755 yet.
The bug causes CI failures occasionally but does not affect other parts of DGL.
As a result, we decide to disable this test until we fixed the bug.
"""
6
import dgl
7
8
import sys
import random
9
10
import time
import numpy as np
11
from numpy.testing import assert_array_equal
Da Zheng's avatar
Da Zheng committed
12
from multiprocessing import Process, Manager
13
14
from scipy import sparse as spsp
import backend as F
15
import unittest
16
import dgl.function as fn
Da Zheng's avatar
Da Zheng committed
17
import traceback
18
from numpy.testing import assert_almost_equal
19

20

21
22
num_nodes = 100
num_edges = int(num_nodes * num_nodes * 0.1)
23
24
rand_port = random.randint(5000, 8000)
print('run graph store with port ' + str(rand_port), file=sys.stderr)
25

26
27
28
def check_array_shared_memory(g, worker_id, arrays):
    if worker_id == 0:
        for i, arr in enumerate(arrays):
29
            arr[0] = i + 10
30
        g._sync_barrier(60)
31
    else:
32
        g._sync_barrier(60)
33
        for i, arr in enumerate(arrays):
34
            assert_almost_equal(F.asnumpy(arr[0]), i + 10)
35

36
def create_graph_store(graph_name):
Da Zheng's avatar
Da Zheng committed
37
38
39
40
    for _ in range(10):
        try:
            g = dgl.contrib.graph_store.create_graph_from_store(graph_name, "shared_mem",
                                                                port=rand_port)
41
42
43
            return g
        except ConnectionError as e:
            traceback.print_exc()
Da Zheng's avatar
Da Zheng committed
44
            time.sleep(1)
45
    return None
46

Da Zheng's avatar
Da Zheng committed
47
def check_init_func(worker_id, graph_name, return_dict):
48
49
50
51
52
53
    time.sleep(3)
    print("worker starts")
    np.random.seed(0)
    csr = (spsp.random(num_nodes, num_nodes, density=0.1, format='csr') != 0).astype(np.int64)

    # Verify the graph structure loaded from the shared memory.
Da Zheng's avatar
Da Zheng committed
54
    try:
55
56
57
58
59
60
61
        g = create_graph_store(graph_name)
        if g is None:
            return_dict[worker_id] = -1
            return

        src, dst = g.all_edges()
        coo = csr.tocoo()
62
63
64
65
66
67
        assert_array_equal(F.asnumpy(dst), coo.row)
        assert_array_equal(F.asnumpy(src), coo.col)
        feat = F.asnumpy(g.nodes[0].data['feat'])
        assert_array_equal(np.squeeze(feat), np.arange(10, dtype=feat.dtype))
        feat = F.asnumpy(g.edges[0].data['feat'])
        assert_array_equal(np.squeeze(feat), np.arange(10, dtype=feat.dtype))
68
69
70
71
72
73
        g.init_ndata('test4', (g.number_of_nodes(), 10), 'float32')
        g.init_edata('test4', (g.number_of_edges(), 10), 'float32')
        g._sync_barrier(60)
        check_array_shared_memory(g, worker_id, [g.nodes[:].data['test4'], g.edges[:].data['test4']])

        data = g.nodes[:].data['test4']
74
75
        g.set_n_repr({'test4': F.ones((1, 10)) * 10}, u=[0])
        assert_almost_equal(F.asnumpy(data[0]), np.squeeze(F.asnumpy(g.nodes[0].data['test4'])))
76
77

        data = g.edges[:].data['test4']
78
79
        g.set_e_repr({'test4': F.ones((1, 10)) * 20}, edges=[0])
        assert_almost_equal(F.asnumpy(data[0]), np.squeeze(F.asnumpy(g.edges[0].data['test4'])))
80
81

        g.destroy()
Da Zheng's avatar
Da Zheng committed
82
83
84
        return_dict[worker_id] = 0
    except Exception as e:
        return_dict[worker_id] = -1
85
86
        g.destroy()
        print(e, file=sys.stderr)
Da Zheng's avatar
Da Zheng committed
87
88
        traceback.print_exc()

89
def server_func(num_workers, graph_name):
90
91
92
93
    print("server starts")
    np.random.seed(0)
    csr = (spsp.random(num_nodes, num_nodes, density=0.1, format='csr') != 0).astype(np.int64)

94
95
    g = dgl.contrib.graph_store.create_graph_store_server(csr, graph_name, "shared_mem", num_workers,
                                                          False, edge_dir="in", port=rand_port)
96
97
    assert num_nodes == g._graph.number_of_nodes()
    assert num_edges == g._graph.number_of_edges()
98
99
100
101
    nfeat = np.arange(0, num_nodes * 10).astype('float32').reshape((num_nodes, 10))
    efeat = np.arange(0, num_edges * 10).astype('float32').reshape((num_edges, 10))
    g.ndata['feat'] = F.tensor(nfeat)
    g.edata['feat'] = F.tensor(efeat)
102
103
    g.run()

104
@unittest.skip
105
def test_init():
Da Zheng's avatar
Da Zheng committed
106
107
    manager = Manager()
    return_dict = manager.dict()
108
    serv_p = Process(target=server_func, args=(2, 'test_graph1'))
Da Zheng's avatar
Da Zheng committed
109
110
    work_p1 = Process(target=check_init_func, args=(0, 'test_graph1', return_dict))
    work_p2 = Process(target=check_init_func, args=(1, 'test_graph1', return_dict))
111
112
113
114
115
116
    serv_p.start()
    work_p1.start()
    work_p2.start()
    serv_p.join()
    work_p1.join()
    work_p2.join()
Da Zheng's avatar
Da Zheng committed
117
118
    for worker_id in return_dict.keys():
        assert return_dict[worker_id] == 0, "worker %d fails" % worker_id
119
120


121
def check_compute_func(worker_id, graph_name, return_dict):
122
123
    time.sleep(3)
    print("worker starts")
Da Zheng's avatar
Da Zheng committed
124
    try:
125
126
127
128
129
130
131
132
133
134
135
        g = create_graph_store(graph_name)
        if g is None:
            return_dict[worker_id] = -1
            return

        g._sync_barrier(60)
        in_feats = g.nodes[0].data['feat'].shape[1]

        # Test update all.
        g.update_all(fn.copy_src(src='feat', out='m'), fn.sum(msg='m', out='preprocess'))
        adj = g.adjacency_matrix()
136
137
        tmp = F.spmm(adj, g.nodes[:].data['feat'])
        assert_almost_equal(F.asnumpy(g.nodes[:].data['preprocess']), F.asnumpy(tmp))
138
139
140
141
142
        g._sync_barrier(60)
        check_array_shared_memory(g, worker_id, [g.nodes[:].data['preprocess']])

        # Test apply nodes.
        data = g.nodes[:].data['feat']
143
144
        g.apply_nodes(func=lambda nodes: {'feat': F.ones((1, in_feats)) * 10}, v=0)
        assert_almost_equal(F.asnumpy(data[0]), np.squeeze(F.asnumpy(g.nodes[0].data['feat'])))
145
146
147

        # Test apply edges.
        data = g.edges[:].data['feat']
148
149
        g.apply_edges(func=lambda edges: {'feat': F.ones((1, in_feats)) * 10}, edges=0)
        assert_almost_equal(F.asnumpy(data[0]), np.squeeze(F.asnumpy(g.edges[0].data['feat'])))
150
151
152
153
154

        g.init_ndata('tmp', (g.number_of_nodes(), 10), 'float32')
        data = g.nodes[:].data['tmp']
        # Test pull
        g.pull(1, fn.copy_src(src='feat', out='m'), fn.sum(msg='m', out='tmp'))
155
        assert_almost_equal(F.asnumpy(data[1]), np.squeeze(F.asnumpy(g.nodes[1].data['preprocess'])))
156
157
158
159

        # Test send_and_recv
        in_edges = g.in_edges(v=2)
        g.send_and_recv(in_edges, fn.copy_src(src='feat', out='m'), fn.sum(msg='m', out='tmp'))
160
        assert_almost_equal(F.asnumpy(data[2]), np.squeeze(F.asnumpy(g.nodes[2].data['preprocess'])))
161
162

        g.destroy()
Da Zheng's avatar
Da Zheng committed
163
164
165
        return_dict[worker_id] = 0
    except Exception as e:
        return_dict[worker_id] = -1
166
167
        g.destroy()
        print(e, file=sys.stderr)
Da Zheng's avatar
Da Zheng committed
168
169
        traceback.print_exc()

170
171

@unittest.skip
172
def test_compute():
Da Zheng's avatar
Da Zheng committed
173
174
    manager = Manager()
    return_dict = manager.dict()
175
    serv_p = Process(target=server_func, args=(2, 'test_graph3'))
Da Zheng's avatar
Da Zheng committed
176
177
    work_p1 = Process(target=check_compute_func, args=(0, 'test_graph3', return_dict))
    work_p2 = Process(target=check_compute_func, args=(1, 'test_graph3', return_dict))
178
179
180
181
182
183
    serv_p.start()
    work_p1.start()
    work_p2.start()
    serv_p.join()
    work_p1.join()
    work_p2.join()
Da Zheng's avatar
Da Zheng committed
184
185
    for worker_id in return_dict.keys():
        assert return_dict[worker_id] == 0, "worker %d fails" % worker_id
186

187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
def check_sync_barrier(worker_id, graph_name, return_dict):
    time.sleep(3)
    print("worker starts")
    try:
        g = create_graph_store(graph_name)
        if g is None:
            return_dict[worker_id] = -1
            return

        if worker_id == 1:
            g.destroy()
            return_dict[worker_id] = 0
            return

        start = time.time()
        try:
            g._sync_barrier(10)
        except TimeoutError as e:
            # this is very loose.
            print("timeout: " + str(abs(time.time() - start)), file=sys.stderr)
            assert 5 < abs(time.time() - start) < 15
        g.destroy()
        return_dict[worker_id] = 0
    except Exception as e:
        return_dict[worker_id] = -1
        g.destroy()
        print(e, file=sys.stderr)
        traceback.print_exc()

216
@unittest.skip
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
def test_sync_barrier():
    manager = Manager()
    return_dict = manager.dict()
    serv_p = Process(target=server_func, args=(2, 'test_graph4'))
    work_p1 = Process(target=check_sync_barrier, args=(0, 'test_graph4', return_dict))
    work_p2 = Process(target=check_sync_barrier, args=(1, 'test_graph4', return_dict))
    serv_p.start()
    work_p1.start()
    work_p2.start()
    serv_p.join()
    work_p1.join()
    work_p2.join()
    for worker_id in return_dict.keys():
        assert return_dict[worker_id] == 0, "worker %d fails" % worker_id

232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
def create_mem(gidx):
    gidx1 = gidx.copyto_shared_mem("in", "test_graph5")
    gidx2 = gidx.copyto_shared_mem("out", "test_graph6")
    time.sleep(30)

def check_mem(gidx):
    time.sleep(10)
    gidx1 = dgl.graph_index.from_shared_mem_csr_matrix("test_graph5", gidx.number_of_nodes(),
                                                       gidx.number_of_edges(), "in", False)
    gidx2 = dgl.graph_index.from_shared_mem_csr_matrix("test_graph6", gidx.number_of_nodes(),
                                                       gidx.number_of_edges(), "out", False)
    in_csr = gidx.adjacency_matrix_scipy(False, "csr")
    out_csr = gidx.adjacency_matrix_scipy(True, "csr")

    in_csr1 = gidx1.adjacency_matrix_scipy(False, "csr")
    assert_array_equal(in_csr.indptr, in_csr1.indptr)
    assert_array_equal(in_csr.indices, in_csr1.indices)
    out_csr1 = gidx1.adjacency_matrix_scipy(True, "csr")
    assert_array_equal(out_csr.indptr, out_csr1.indptr)
    assert_array_equal(out_csr.indices, out_csr1.indices)

    in_csr2 = gidx2.adjacency_matrix_scipy(False, "csr")
    assert_array_equal(in_csr.indptr, in_csr2.indptr)
    assert_array_equal(in_csr.indices, in_csr2.indices)
    out_csr2 = gidx2.adjacency_matrix_scipy(True, "csr")
    assert_array_equal(out_csr.indptr, out_csr2.indptr)
    assert_array_equal(out_csr.indices, out_csr2.indices)

    gidx1 = gidx1.copyto_shared_mem("in", "test_graph5")
    gidx2 = gidx2.copyto_shared_mem("out", "test_graph6")

263
@unittest.skip
264
265
266
267
268
269
270
271
272
273
def test_copy_shared_mem():
    csr = (spsp.random(num_nodes, num_nodes, density=0.1, format='csr') != 0).astype(np.int64)
    gidx = dgl.graph_index.create_graph_index(csr, False, True)
    p1 = Process(target=create_mem, args=(gidx,))
    p2 = Process(target=check_mem, args=(gidx,))
    p1.start()
    p2.start()
    p1.join()
    p2.join()

274
if __name__ == '__main__':
275
    test_copy_shared_mem()
276
    test_init()
277
    test_sync_barrier()
278
    test_compute()