COMBINATORIAL_BLAS 1.6
 
Loading...
Searching...
No Matches
fast_map.h
Go to the documentation of this file.
1/*
2//@HEADER
3// *****************************************************************************
4//
5// HPCGraph: Graph Computation on High Performance Computing Systems
6// Copyright (2016) Sandia Corporation
7//
8// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9// the U.S. Government retains certain rights in this software.
10//
11// Redistribution and use in source and binary forms, with or without
12// modification, are permitted provided that the following conditions are
13// met:
14//
15// 1. Redistributions of source code must retain the above copyright
16// notice, this list of conditions and the following disclaimer.
17//
18// 2. Redistributions in binary form must reproduce the above copyright
19// notice, this list of conditions and the following disclaimer in the
20// documentation and/or other materials provided with the distribution.
21//
22// 3. Neither the name of the Corporation nor the names of the
23// contributors may be used to endorse or promote products derived from
24// this software without specific prior written permission.
25//
26// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37//
38// Questions? Contact George M. Slota (gmslota@sandia.gov)
39// Siva Rajamanickam (srajama@sandia.gov)
40// Kamesh Madduri (madduri@cse.psu.edu)
41//
42// *****************************************************************************
43//@HEADER
44*/
45
46#ifndef _FAST_MAP_H_
47#define _FAST_MAP_H_
48
49#include <omp.h>
50#include <stdio.h>
51#include <stdlib.h>
52#include <stdint.h>
53#include <vector>
54
55extern int procid, nprocs;
56extern bool verbose, debug, verify, output;
57
58#define NULL_KEY 18446744073709551615
59
68
69bool is_init(fast_map* map);
70void init_map(fast_map* map);
71void init_map(fast_map* map, uint64_t init_size);
72void init_map_nohash(fast_map* map, uint64_t init_size);
73void clear_map(fast_map* map);
74void empty_map(fast_map* map);
75
76inline uint64_t mult_hash(fast_map* map, uint64_t key);
77inline void set_value(fast_map* map, uint64_t key, uint64_t value);
78inline void set_value_uq(fast_map* map, uint64_t key, uint64_t value);
79inline uint64_t get_value(fast_map* map, uint64_t key);
80inline uint64_t get_max_key(fast_map* map);
81
83{
84 if (map->hashing)
85 return (key*2654435761 % map->capacity);
86 else
87 return key;
88}
89
90inline void set_value(fast_map* map, uint64_t key, uint64_t value)
91{
92 uint64_t cur_index = mult_hash(map, key)*2;
93 uint64_t count = 0;
94 while (map->arr[cur_index] != key && map->arr[cur_index] != NULL_KEY)
95 {
96 cur_index = (cur_index + 2) % (map->capacity*2);
97 ++count;
98 if (debug && count % 100 == 0)
99 fprintf(stderr, "Warning: fast_map set_value(): Big Count %d -- %lu - %lu, %lu, %lu\n",
100 procid, count, cur_index, key, value);
101 }
102 if (map->arr[cur_index] == NULL_KEY)
103 {
104 map->arr[cur_index] = key;
105 }
106 map->arr[cur_index+1] = value;
107}
108
109inline void set_value_uq(fast_map* map, uint64_t key, uint64_t value)
110{
111 uint64_t cur_index = mult_hash(map, key)*2;
112 uint64_t count = 0;
113 while (map->arr[cur_index] != key && map->arr[cur_index] != NULL_KEY)
114 {
115 cur_index = (cur_index + 2) % (map->capacity*2);
116 ++count;
117 if (debug && count % 100 == 0)
118 fprintf(stderr, "Warning: fast_map set_value_uq(): Big Count %d -- %lu - %lu, %lu, %lu\n",
119 procid, count, cur_index, key, value);
120 }
121 if (map->arr[cur_index] == NULL_KEY)
122 {
123 map->arr[cur_index] = key;
124 map->unique_keys[map->num_unique] = key;
125 map->unique_indexes[map->num_unique] = cur_index;
126 ++map->num_unique;
127 }
128 map->arr[cur_index+1] = value;
129}
130
131
133{
134 uint64_t cur_index = mult_hash(map, key)*2;
135 while (map->arr[cur_index] != key && map->arr[cur_index] != NULL_KEY)
136 cur_index = (cur_index + 2) % (map->capacity*2);
137 if (map->arr[cur_index] == NULL_KEY)
138 return NULL_KEY;
139 else
140 return map->arr[cur_index+1];
141}
142
144{
145 uint64_t max_val = 0;
146 uint64_t max_key = NULL_KEY;
147 std::vector<uint64_t> vec;
148 for (uint64_t i = 0; i < map->num_unique; ++i)
149 if (map->arr[map->unique_indexes[i]+1] > max_val)
150 {
151 max_val = map->arr[map->unique_indexes[i]+1];
152 vec.clear();
153 vec.push_back(map->arr[map->unique_indexes[i]]);
154 }
155 else if (map->arr[map->unique_indexes[i]+1] == max_val)
156 {
157 vec.push_back(map->arr[map->unique_indexes[i]]);
158 }
159
160 if (vec.size() > 0)
161 max_key = vec[(int)rand() % vec.size()];
162
163 return max_key;
164}
165
166
167#endif
#define NULL_KEY
Definition fast_map.h:58
uint64_t get_max_key(fast_map *map)
Definition fast_map.h:143
void set_value(fast_map *map, uint64_t key, uint64_t value)
Definition fast_map.h:90
void clear_map(fast_map *map)
Definition fast_map.cpp:121
uint64_t get_value(fast_map *map, uint64_t key)
Definition fast_map.h:132
int procid
Definition main.cpp:55
void init_map_nohash(fast_map *map, uint64_t init_size)
Definition fast_map.cpp:90
bool debug
Definition fast_map.h:56
bool verify
Definition fast_map.h:56
void set_value_uq(fast_map *map, uint64_t key, uint64_t value)
Definition fast_map.h:109
void empty_map(fast_map *map)
Definition fast_map.cpp:131
uint64_t mult_hash(fast_map *map, uint64_t key)
Definition fast_map.h:82
bool is_init(fast_map *map)
Definition fast_map.cpp:58
bool verbose
Definition main.cpp:56
bool output
Definition fast_map.h:56
int nprocs
Definition fast_map.h:55
void init_map(fast_map *map)
Definition fast_map.cpp:63
unsigned __int64 uint64_t
Definition stdint.h:90
bool hashing
Definition fast_map.h:66
uint64_t * unique_keys
Definition fast_map.h:62
uint64_t num_unique
Definition fast_map.h:65
uint64_t capacity
Definition fast_map.h:64
uint64_t * unique_indexes
Definition fast_map.h:63
uint64_t * arr
Definition fast_map.h:61