001: /* ///////////////////////////// P /// L /// A /// S /// M /// A /////////////////////////////// */
002: /* ///                    PLASMA auxiliary routines (version 2.1.0)                          ///
003:  * ///                    Author: Jakub Kurzak                                               ///
004:  * ///                    Release Date: November, 15th 2009                                  ///
005:  * ///                    PLASMA is a software package provided by Univ. of Tennessee,       ///
006:  * ///                    Univ. of California Berkeley and Univ. of Colorado Denver          /// */
007: /* ///////////////////////////////////////////////////////////////////////////////////////////// */
008: #include "common.h"
009: #include "allocate.h"
010: #include "auxiliary.h"
011: #include "workspace.h"
012: 
013: #include <stdlib.h>
014: #include <malloc.h>
015: 
016: /* ///////////////////////////////////////////////////////////////////////////////////////////// */
017: int plasma_alloc_ibnb(int M, int N, int operation, int type, void **memptr)
018: {
019:     size_t size;
020:     int status;
021:     int NB, MT, NT;
022:     plasma_context_t *plasma;
023: 
024:     plasma = plasma_context_self();
025:     if (plasma == NULL) {
026:         plasma_fatal_error("plasma_alloc_ibnb", "PLASMA not initialized");
027:         return PLASMA_ERR_NOT_INITIALIZED;
028:     }
029:     /* Tune NB & IB depending on M & N; Set IBNBSIZE */
030:     status = plasma_tune(operation, M, N, 0);
031:     if (status != PLASMA_SUCCESS) {
032:         plasma_error("plasma_alloc_ibnb", "plasma_tune() failed");
033:         return PLASMA_ERR_UNEXPECTED;
034:     }
035:     /* Set MT & NT & allocate */
036:     NB = PLASMA_NB;
037:     NT = (N%NB==0) ? (N/NB) : (N/NB+1);
038:     MT = (M%NB==0) ? (M/NB) : (M/NB+1);
039:     size = MT*NT * PLASMA_IBNBSIZE * plasma_element_size(type);
040:     if (size <= 0) {
041:         *memptr = NULL;
042:         return PLASMA_SUCCESS;
043:     }
044:     //status = posix_memalign(memptr, STANDARD_PAGE_SIZE, size);
045:     *memptr = malloc(size);
046:   //if (status != 0) {
047:     if (*memptr == NULL) {
048:         plasma_error("plasma_alloc_ibnb", "posix_memalign() failed");
049:         return PLASMA_ERR_OUT_OF_RESOURCES;
050:     }
051:     return PLASMA_SUCCESS;
052: }
053: 
054: /* ///////////////////////////////////////////////////////////////////////////////////////////// */
055: int plasma_alloc_ipiv(int M, int N, int operation, void **memptr)
056: {
057:     size_t size;
058:     int status;
059:     int NB, MT, NT;
060:     plasma_context_t *plasma;
061: 
062:     plasma = plasma_context_self();
063:     if (plasma == NULL) {
064:         plasma_fatal_error("plasma_alloc_ipiv", "PLASMA not initialized");
065:         return PLASMA_ERR_NOT_INITIALIZED;
066:     }
067:     /* Tune NB & IB depending on M & N; Set IBNBSIZE */
068:     status = plasma_tune(operation, M, N, 0);
069:     if (status != PLASMA_SUCCESS) {
070:         plasma_error("plasma_alloc_ipiv", "plasma_tune() failed");
071:         return PLASMA_ERR_UNEXPECTED;
072:     }
073:     /* Set MT & NT & allocate */
074:     NB = PLASMA_NB;
075:     NT = (N%NB==0) ? (N/NB) : (N/NB+1);
076:     MT = (M%NB==0) ? (M/NB) : (M/NB+1);
077:     size = MT*NT * NB * sizeof(int);
078:     if (size <= 0) {
079:         *memptr = NULL;
080:         return PLASMA_SUCCESS;
081:     }
082:   //status = posix_memalign(memptr, CACHE_LINE_SIZE, size);
083:     *memptr = malloc(size);
084:   //if (status != 0) {
085:     if (*memptr == NULL) {
086:         plasma_error("plasma_alloc_ipiv", "posix_memalign() failed");
087:         return PLASMA_ERR_OUT_OF_RESOURCES;
088:     }
089:     return PLASMA_SUCCESS;
090: }
091: 
092: /* /////////////////////////// P /// U /// R /// P /// O /// S /// E /////////////////////////// */
093: // PLASMA_Dealloc_Handle - Deallocate workspace handle allocated by any workspace allocation
094: // routine.
095: 
096: /* ///////////////////// A /// R /// G /// U /// M /// E /// N /// T /// S ///////////////////// */
097: // handle   void** (IN)
098: //          Workspace handle
099: 
100: /* ///////////// R /// E /// T /// U /// R /// N /////// V /// A /// L /// U /// E ///////////// */
101: //          = PLASMA_SUCCESS: successful exit
102: 
103: /* //////////////////////////////////// C /// O /// D /// E //////////////////////////////////// */
104: int PLASMA_Dealloc_Handle(void **handle)
105: {
106:     plasma_context_t *plasma;
107: 
108:     plasma = plasma_context_self();
109:     if (plasma == NULL) {
110:         plasma_fatal_error("PLASMA_Dealloc_Handle", "PLASMA not initialized");
111:         return PLASMA_ERR_NOT_INITIALIZED;
112:     }
113:     if (*handle == NULL) {
114:         plasma_error("PLASMA_Dealloc_Handle", "attempting to deallocate a NULL handle");
115:         return PLASMA_ERR_UNALLOCATED;
116:     }
117:     free(*handle);
118:     *handle = NULL;
119:     return PLASMA_SUCCESS;
120: }
121: