Galois
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
EdgeContext.h
Go to the documentation of this file.
1 /*
2  * This file belongs to the Galois project, a C++ library for exploiting
3  * parallelism. The code is being released under the terms of the 3-Clause BSD
4  * License (a copy is located in LICENSE.txt at the top-level directory).
5  *
6  * Copyright (C) 2018, The University of Texas at Austin. All rights reserved.
7  * UNIVERSITY EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES CONCERNING THIS
8  * SOFTWARE AND DOCUMENTATION, INCLUDING ANY WARRANTIES OF MERCHANTABILITY,
9  * FITNESS FOR ANY PARTICULAR PURPOSE, NON-INFRINGEMENT AND WARRANTIES OF
10  * PERFORMANCE, AND ANY WARRANTY THAT MIGHT OTHERWISE ARISE FROM COURSE OF
11  * DEALING OR USAGE OF TRADE. NO WARRANTY IS EITHER EXPRESS OR IMPLIED WITH
12  * RESPECT TO THE USE OF THE SOFTWARE OR DOCUMENTATION. Under no circumstances
13  * shall University be liable for incidental, special, indirect, direct or
14  * consequential damages or loss of profits, interruption of business, or
15  * related expenses which may arise from use of Software or Documentation,
16  * including but not limited to those resulting from defects in Software and/or
17  * Documentation, or loss or inaccuracy of data of any kind.
18  */
19 
28 #pragma once
29 
30 #pragma once
31 #include <cuda.h>
32 #include "gg.h"
33 #pragma once
35 
37  unsigned int* num_edges; // per host
38  DeviceOnly<unsigned int>* edges; // per host
39 };
40 
42  int device;
43  int id;
44  unsigned int numOwned; // Number of nodes owned (masters) by this host
45  unsigned int beginMaster; // local id of the beginning of master nodes
46  unsigned int numNodesWithEdges; // Number of nodes (masters + mirrors) that
47  // have outgoing edges
48  CSRGraphTy gg;
51  DeviceOnly<unsigned int> offsets; // union across master/mirror of all hosts
52  Shared<DynamicBitset> is_updated; // union across master/mirror of all hosts
53 };
54 
55 template <typename Type>
57  Shared<Type> data;
58  Shared<DynamicBitset> is_updated; // size of edges
59  DeviceOnly<Type> shared_data; // union across master/mirror of all hosts
60 };
61 
63  int device) {
64  struct cudaDeviceProp dev;
65  if (device == -1) {
66  check_cuda(cudaGetDevice(&device));
67  } else {
68  int count;
69  check_cuda(cudaGetDeviceCount(&count));
70  if (device > count) {
71  fprintf(stderr, "Error: Out-of-range GPU %d specified (%d total GPUs)",
72  device, count);
73  return false;
74  }
75  check_cuda(cudaSetDevice(device));
76  }
77  ctx->device = device;
78  check_cuda(cudaGetDeviceProperties(&dev, device));
79  printf("[%d] Using GPU %d: %s\n", ctx->id, device, dev.name);
80  return true;
81 }
82 
84  EdgeMarshalGraph& g, unsigned num_hosts,
85  bool LoadProxyEdges = true) {
86  CSRGraphTy graph;
87  ctx->numOwned = g.numOwned;
88  ctx->beginMaster = g.beginMaster;
90  assert(ctx->id == g.id);
91 
92  size_t mem_usage = ((g.nnodes + 1) + g.nedges) * sizeof(index_type) +
93  (g.nnodes) * sizeof(node_data_type);
94  if (!g.edge_data)
95  mem_usage += (g.nedges) * sizeof(edge_data_type);
96  printf("[%d] Host memory for graph: %3u MB\n", ctx->id, mem_usage / 1048756);
97 
98  // copy the graph to the GPU
99  graph.nnodes = g.nnodes;
100  graph.nedges = g.nedges;
101  graph.row_start = g.row_start;
102  graph.edge_dst = g.edge_dst;
103  graph.node_data = g.node_data;
104  graph.edge_data = g.edge_data;
105  graph.copy_to_gpu(ctx->gg);
106 
107  if (LoadProxyEdges) {
108  size_t max_shared_size = 0; // for union across master/mirror of all hosts
109  ctx->master.num_edges =
110  (unsigned int*)calloc(num_hosts, sizeof(unsigned int));
111  memcpy(ctx->master.num_edges, g.num_master_edges,
112  sizeof(unsigned int) * num_hosts);
113  ctx->master.edges = (DeviceOnly<unsigned int>*)calloc(
114  num_hosts, sizeof(Shared<unsigned int>));
115  for (uint32_t h = 0; h < num_hosts; ++h) {
116  if (ctx->master.num_edges[h] > 0) {
117  ctx->master.edges[h].alloc(ctx->master.num_edges[h]);
118  ctx->master.edges[h].copy_to_gpu(g.master_edges[h],
119  ctx->master.num_edges[h]);
120  }
121  if (ctx->master.num_edges[h] > max_shared_size) {
122  max_shared_size = ctx->master.num_edges[h];
123  }
124  }
125  ctx->mirror.num_edges =
126  (unsigned int*)calloc(num_hosts, sizeof(unsigned int));
127  memcpy(ctx->mirror.num_edges, g.num_mirror_edges,
128  sizeof(unsigned int) * num_hosts);
129  ctx->mirror.edges = (DeviceOnly<unsigned int>*)calloc(
130  num_hosts, sizeof(Shared<unsigned int>));
131  for (uint32_t h = 0; h < num_hosts; ++h) {
132  if (ctx->mirror.num_edges[h] > 0) {
133  ctx->mirror.edges[h].alloc(ctx->mirror.num_edges[h]);
134  ctx->mirror.edges[h].copy_to_gpu(g.mirror_edges[h],
135  ctx->mirror.num_edges[h]);
136  }
137  if (ctx->mirror.num_edges[h] > max_shared_size) {
138  max_shared_size = ctx->mirror.num_edges[h];
139  }
140  }
141  ctx->offsets.alloc(max_shared_size);
142  ctx->is_updated.alloc(1);
143  ctx->is_updated.cpu_wr_ptr()->alloc(max_shared_size);
144  }
145  // printf("[%u] load_graph_GPU: %u owned nodes of total %u resident, %lu
146  // edges\n", ctx->id, ctx->nowned, graph.nnodes, graph.nedges);
147 }
148 
149 size_t mem_usage_CUDA_common_edges(EdgeMarshalGraph& g, unsigned num_hosts) {
150  size_t mem_usage = 0;
151  size_t max_shared_size = 0; // for union across master/mirror of all hosts
152  mem_usage += num_hosts * sizeof(unsigned int);
153  mem_usage += num_hosts * sizeof(Shared<unsigned int>);
154  for (uint32_t h = 0; h < num_hosts; ++h) {
155  if (g.num_master_edges[h] > 0) {
156  mem_usage += g.num_master_edges[h] * sizeof(unsigned int);
157  }
158  if (g.num_master_edges[h] > max_shared_size) {
159  max_shared_size = g.num_master_edges[h];
160  }
161  }
162  mem_usage += num_hosts * sizeof(unsigned int);
163  mem_usage += num_hosts * sizeof(Shared<unsigned int>);
164  for (uint32_t h = 0; h < num_hosts; ++h) {
165  if (g.num_mirror_edges[h] > 0) {
166  mem_usage += g.num_mirror_edges[h] * sizeof(unsigned int);
167  }
168  if (g.num_mirror_edges[h] > max_shared_size) {
169  max_shared_size = g.num_mirror_edges[h];
170  }
171  }
172  mem_usage += max_shared_size * sizeof(unsigned int);
173  mem_usage += ((max_shared_size + 63) / 64) * sizeof(unsigned long long int);
174  return mem_usage;
175 }
176 
177 template <typename Type>
179  struct CUDA_Context_Field_Edges<Type>* field,
180  unsigned num_hosts) {
181  field->data.alloc(ctx->gg.nedges);
182  size_t max_shared_size = 0; // for union across master/mirror of all hosts
183  for (uint32_t h = 0; h < num_hosts; ++h) {
184  if (ctx->master.num_edges[h] > max_shared_size) {
185  max_shared_size = ctx->master.num_edges[h];
186  }
187  }
188  for (uint32_t h = 0; h < num_hosts; ++h) {
189  if (ctx->mirror.num_edges[h] > max_shared_size) {
190  max_shared_size = ctx->mirror.num_edges[h];
191  }
192  }
193  field->shared_data.alloc(max_shared_size);
194  field->is_updated.alloc(1);
195  field->is_updated.cpu_wr_ptr()->alloc(ctx->gg.nedges);
196 }
197 
198 template <typename Type>
200  EdgeMarshalGraph& g, unsigned num_hosts) {
201  size_t mem_usage = 0;
202  mem_usage += g.nedges * sizeof(Type);
203  size_t max_shared_size = 0; // for union across master/mirror of all hosts
204  for (uint32_t h = 0; h < num_hosts; ++h) {
205  if (g.num_master_edges[h] > max_shared_size) {
206  max_shared_size = g.num_master_edges[h];
207  }
208  }
209  for (uint32_t h = 0; h < num_hosts; ++h) {
210  if (g.num_mirror_edges[h] > max_shared_size) {
211  max_shared_size = g.num_mirror_edges[h];
212  }
213  }
214  mem_usage += max_shared_size * sizeof(Type);
215  mem_usage += ((g.nedges + 63) / 64) * sizeof(unsigned long long int);
216  return mem_usage;
217 }
void load_graph_CUDA_common_edges(struct CUDA_Context_Common_Edges *ctx, EdgeMarshalGraph &g, unsigned num_hosts, bool LoadProxyEdges=true)
Definition: EdgeContext.h:83
unsigned int * num_master_edges
Definition: EdgeHostDecls.h:51
unsigned int numNodesWithEdges
Definition: EdgeContext.h:46
size_t nedges
Definition: EdgeHostDecls.h:40
unsigned int numOwned
Definition: EdgeHostDecls.h:41
unsigned int ** mirror_edges
Definition: EdgeHostDecls.h:54
unsigned int ** master_edges
Definition: EdgeHostDecls.h:52
index_type * row_start
Definition: EdgeHostDecls.h:47
Shared< Type > data
Definition: EdgeContext.h:57
DeviceOnly< Type > shared_data
Definition: EdgeContext.h:59
Definition: EdgeContext.h:36
unsigned int index_type
Definition: EdgeHostDecls.h:33
Contains forward declarations and the definition of the EdgeMarshalGraph class, which is used to mars...
Definition: EdgeContext.h:56
unsigned int numNodesWithEdges
Definition: EdgeHostDecls.h:43
struct CUDA_Context_Shared_Edges mirror
Definition: EdgeContext.h:50
int id
Definition: EdgeHostDecls.h:45
size_t mem_usage_CUDA_common_edges(EdgeMarshalGraph &g, unsigned num_hosts)
Definition: EdgeContext.h:149
unsigned int * num_mirror_edges
Definition: EdgeHostDecls.h:53
unsigned int beginMaster
Definition: EdgeContext.h:45
Shared< DynamicBitset > is_updated
Definition: EdgeContext.h:52
node_data_type * node_data
Definition: EdgeHostDecls.h:49
int device
Definition: EdgeContext.h:42
DeviceOnly< unsigned int > offsets
Definition: EdgeContext.h:51
unsigned int * num_edges
Definition: EdgeContext.h:37
void load_graph_CUDA_field_edges(struct CUDA_Context_Common_Edges *ctx, struct CUDA_Context_Field_Edges< Type > *field, unsigned num_hosts)
Definition: EdgeContext.h:178
unsigned int beginMaster
Definition: EdgeHostDecls.h:42
index_type * edge_dst
Definition: EdgeHostDecls.h:48
unsigned int numOwned
Definition: EdgeContext.h:44
CSRGraphTy gg
Definition: EdgeContext.h:48
struct CUDA_Context_Shared_Edges master
Definition: EdgeContext.h:49
unsigned int node_data_type
Definition: EdgeHostDecls.h:34
size_t nnodes
Definition: EdgeHostDecls.h:39
size_t mem_usage_CUDA_field_edges(struct CUDA_Context_Field_Edges< Type > *field, EdgeMarshalGraph &g, unsigned num_hosts)
Definition: EdgeContext.h:199
DeviceOnly< unsigned int > * edges
Definition: EdgeContext.h:38
bool init_CUDA_context_common_edges(struct CUDA_Context_Common_Edges *ctx, int device)
Definition: EdgeContext.h:62
Definition: EdgeContext.h:41
int id
Definition: EdgeContext.h:43
edge_data_type * edge_data
Definition: EdgeHostDecls.h:50
unsigned edge_data_type
Definition: EdgeHostDecls.h:35
Definition: EdgeHostDecls.h:38
Shared< DynamicBitset > is_updated
Definition: EdgeContext.h:58