PLASMA  2.4.5
PLASMA - Parallel Linear Algebra for Scalable Multi-core Architectures
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
context.c
Go to the documentation of this file.
1 
14 #include <stdlib.h>
15 #if defined( _WIN32 ) || defined( _WIN64 )
16 #include "plasmawinthread.h"
17 #else
18 #include <pthread.h>
19 #endif
20 
21 #include "common.h"
22 #include "auxiliary.h"
23 #include "context.h"
24 
25 /***************************************************************************/
28 /* master threads context lookup table */
30 /* context lookup table access lock */
32 
33 /***************************************************************************/
37 {
39 
40  plasma = (plasma_context_t*)malloc(sizeof(plasma_context_t));
41  if (plasma == NULL) {
42  plasma_fatal_error("plasma_context_create", "malloc() failed");
43  return NULL;
44  }
45 
46  pthread_mutex_init(&plasma->action_mutex, NULL);
48  pthread_cond_init(&plasma->action_condt, NULL);
49  plasma->action = PLASMA_ACT_STAND_BY;
50  plasma->parallel_func_ptr = NULL;
51 
52  /* These initializations are just in case the user
53  disables autotuning and does not set nb and ib */
54  plasma->nb = 128;
55  plasma->ib = 32;
56  plasma->nbnbsize = 16384;
57  plasma->ibnbsize = 4096;
58  plasma->rhblock = 4;
59 
60  plasma->errors_enabled = PLASMA_TRUE;
61  plasma->warnings_enabled = PLASMA_TRUE;
63  plasma->dynamic_section = PLASMA_FALSE;
64 
68 
69  return plasma;
70 }
71 
72 /***************************************************************************/
76 {
77  int i;
78 
79  // acquire the access lock
81  // For each entry
82  for (i = 0; i < CONTEXTS_MAX; i++) {
83  // If not occupied
84  if (context_map[i].context == NULL) {
85  // Insert new context, release lock, return success
86  context_map[i].context = context;
87  context_map[i].thread_id = thread_id;
89  return PLASMA_SUCCESS;
90  }
91  }
92  // No empty entry found - release lock, return error
94  plasma_fatal_error("plasma_context_insert", "too many threads");
96 }
97 
98 /***************************************************************************/
102 {
103  int i;
104 
105  // acquire the access lock
107  // For each entry
108  for (i = 0; i < CONTEXTS_MAX; i++) {
109  // If id matches
110  if (pthread_equal(context_map[i].thread_id, thread_id)) {
111  if (context_map[i].context == context) {
112  // Free the context, mark entry as empty, release lock, return success
113  free(context_map[i].context);
114  context_map[i].context = NULL;
116  return PLASMA_SUCCESS;
117  }
118  else {
120  plasma_fatal_error("plasma_context_remove", "context does not match thread");
121  return PLASMA_ERR_UNEXPECTED;
122  }
123  }
124  }
125  // No matching id found - release lock, return error
127  plasma_fatal_error("plasma_context_remove", "thread not found");
128  return PLASMA_ERR_NOT_FOUND;
129 }
130 
131 /***************************************************************************/
135 {
136  int i;
137 
138  // For each entry
139  for (i = 0; i < CONTEXTS_MAX; i++) {
140  // If id matches
141  if (pthread_equal(context_map[i].thread_id, pthread_self())) {
142  return context_map[i].context;
143  }
144  }
145  return NULL;
146 }
147 
148 /***************************************************************************/
170 {
172 
173  plasma = plasma_context_self();
174  if (plasma == NULL) {
175  plasma_fatal_error("PLASMA_Enable", "PLASMA not initialized");
177  }
178 
179  switch (lever)
180  {
181  case PLASMA_WARNINGS:
182  plasma->warnings_enabled = PLASMA_TRUE;
183  break;
184  case PLASMA_ERRORS:
185  plasma->errors_enabled = PLASMA_TRUE;
186  break;
187  case PLASMA_AUTOTUNING:
189  break;
190  case PLASMA_DAG:
191  QUARK_Barrier(plasma->quark);
192  QUARK_DOT_DAG_Enable( plasma->quark, 1);
193  break;
194  default:
195  plasma_error("PLASMA_Enable", "illegal parameter value");
197  }
198  return PLASMA_SUCCESS;
199 }
200 
201 /***************************************************************************/
223 {
225 
226  plasma = plasma_context_self();
227  if (plasma == NULL) {
228  plasma_fatal_error("PLASMA_Disable", "PLASMA not initialized");
230  }
231  switch (lever)
232  {
233  case PLASMA_WARNINGS:
234  plasma->warnings_enabled = PLASMA_FALSE;
235  break;
236  case PLASMA_ERRORS:
237  plasma->errors_enabled = PLASMA_FALSE;
238  break;
239  case PLASMA_AUTOTUNING:
241  break;
242  case PLASMA_DAG:
243  QUARK_Barrier(plasma->quark);
244  QUARK_DOT_DAG_Enable( plasma->quark, 0);
245  break;
246  default:
247  plasma_error("PLASMA_Disable", "illegal parameter value");
249  }
250  return PLASMA_SUCCESS;
251 }
252 
253 /***************************************************************************/
278 int PLASMA_Set(PLASMA_enum param, int value)
279 {
281 
282  plasma = plasma_context_self();
283  if (plasma == NULL) {
284  plasma_error("PLASMA_Set", "PLASMA not initialized");
286  }
287  switch (param) {
288  case PLASMA_TILE_SIZE:
289  if (value <= 0) {
290  plasma_error("PLASMA_Set", "negative tile size");
292  }
293  plasma->nb = value;
294  /* Calculate A, B tile size and round up to cache line size */
295  /* round up for the smallest type (float) - will hold for all */
296  plasma->nbnbsize = plasma->nb * plasma->nb; // * sizeof(float);
297  plasma->ibnbsize = plasma->ib * plasma->nb; // * sizeof(float);
298 // plasma->nbnbsize = roundup(plasma->nbnbsize, CACHE_LINE_SIZE);
299 // plasma->nbnbsize /= sizeof(float);
300  if ( plasma->autotuning_enabled ) {
302  plasma_warning("PLASMA_Set", "autotuning has been automatically disabled\n");
303  }
304  /* Limit ib to nb */
305  plasma->ib = min( plasma->nb, plasma->ib );
306  break;
308  if (value <= 0) {
309  plasma_error("PLASMA_Set", "negative inner block size");
311  }
312  if (value > plasma->nb) {
313  plasma_error("PLASMA_Set", "inner block larger than tile");
315  }
316  /* if (plasma->nb % value != 0) { */
317  /* plasma_error("PLASMA_Set", "inner block does not divide tile"); */
318  /* return PLASMA_ERR_ILLEGAL_VALUE; */
319  /* } */
320  plasma->ib = value;
321  /* Calculate T, L tile size and round up to cache line size */
322  /* round up for the smallest type (float) - will hold for all */
323  plasma->ibnbsize = plasma->ib * plasma->nb; // * sizeof(float);
324 // plasma->ibnbsize = roundup(plasma->ibnbsize, CACHE_LINE_SIZE);
325 // plasma->ibnbsize /= sizeof(float);
326 
327  if ( plasma->autotuning_enabled ) {
329  plasma_warning("PLASMA_Set", "autotuning has been automatically disabled\n");
330  }
331  break;
333  if (value != PLASMA_STATIC_SCHEDULING && value != PLASMA_DYNAMIC_SCHEDULING) {
334  plasma_error("PLASMA_Set", "illegal value of PLASMA_SCHEDULING_MODE");
336  }
337  plasma->scheduling = value;
338  break;
340  if (value != PLASMA_FLAT_HOUSEHOLDER && value != PLASMA_TREE_HOUSEHOLDER) {
341  plasma_error("PLASMA_Set", "illegal value of PLASMA_HOUSEHOLDER_MODE");
343  }
344  plasma->householder = value;
345  break;
347  if (value <= 0) {
348  plasma_error("PLASMA_Set", "negative householder size");
350  }
351  plasma->rhblock = value;
352  break;
354  if (value != PLASMA_INPLACE && value != PLASMA_OUTOFPLACE) {
355  plasma_error("PLASMA_Set", "illegal value of PLASMA_TRANSLATION_MODE");
357  }
358  plasma->translation = value;
359  break;
360  default:
361  plasma_error("PLASMA_Set", "unknown parameter");
363  }
364  return PLASMA_SUCCESS;
365 }
366 
367 /***************************************************************************/
391 int PLASMA_Get(PLASMA_enum param, int *value)
392 {
394 
395  plasma = plasma_context_self();
396  if (plasma == NULL) {
397  plasma_fatal_error("PLASMA_Get", "PLASMA not initialized");
399  }
400  switch (param) {
401  case PLASMA_TILE_SIZE:
402  *value = plasma->nb;
403  return PLASMA_SUCCESS;
405  *value = plasma->ib;
406  return PLASMA_SUCCESS;
408  *value = plasma->scheduling;
409  return PLASMA_SUCCESS;
411  *value = plasma->householder;
412  return PLASMA_SUCCESS;
414  *value = plasma->rhblock;
415  return PLASMA_SUCCESS;
417  *value = plasma->translation;
418  return PLASMA_SUCCESS;
419  default:
420  plasma_error("PLASMA_Get", "unknown parameter");
422  }
423 }