workspace.c 5.79 KB
Newer Older
1 2
/**
 *
3 4
 * @copyright (c) 2009-2014 The University of Tennessee and The University
 *                          of Tennessee Research Foundation.
5 6
 *                          All rights reserved.
 * @copyright (c) 2012-2014 Inria. All rights reserved.
7
 * @copyright (c) 2012-2014 Bordeaux INP, CNRS (LaBRI UMR 5800), Inria, Univ. Bordeaux. All rights reserved.
8 9 10 11 12 13 14 15 16 17 18
 *
 **/

/**
 *
 * @file workspace.c
 *
 *  MORSE auxiliary routines
 *  MORSE is a software package provided by Univ. of Tennessee,
 *  Univ. of California Berkeley and Univ. of Colorado Denver
 *
19
 * @version 0.9.0
20 21 22 23 24
 * @author Mathieu Faverge
 * @author Cedric Castagnede
 * @date 2012-09-15
 *
 **/
25 26 27 28 29 30 31 32

/**
 *
 * @defgroup Workspace
 * @brief Group routines exposed to users about specific workspaces management
 *
 */

33
#include <stdlib.h>
34 35 36
#include "control/common.h"
#include "control/auxiliary.h"
#include "control/workspace.h"
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136

/*******************************************************************************
 *
 **/
int morse_alloc_ibnb_tile(int M, int N, MORSE_enum func, int type, MORSE_desc_t **desc)
{
    int status;
    int IB, NB, MT, NT;
    int64_t lm, ln;
    MORSE_context_t *morse;

    morse = morse_context_self();
    if (morse == NULL) {
        morse_fatal_error("morse_alloc_ibnb_tile", "MORSE not initialized");
        return MORSE_ERR_NOT_INITIALIZED;
    }

    /* Tune NB & IB depending on M & N; Set IBNBSIZE */
    status = morse_tune(func, M, N, 0);
    if (status != MORSE_SUCCESS) {
        morse_error("morse_alloc_ibnb_tile", "morse_tune() failed");
        return MORSE_ERR_UNEXPECTED;
    }

    /* Set MT & NT & allocate */
    NB = MORSE_NB;
    IB = MORSE_IB;
    MT = (M%NB==0) ? (M/NB) : (M/NB+1);
    NT = (N%NB==0) ? (N/NB) : (N/NB+1);

    /* Size is doubled for RH QR to store the reduction T */
    if ((morse->householder == MORSE_TREE_HOUSEHOLDER) &&
        ((func == MORSE_FUNC_SGELS)  ||
         (func == MORSE_FUNC_DGELS)  ||
         (func == MORSE_FUNC_CGELS)  ||
         (func == MORSE_FUNC_ZGELS)  ||
         (func == MORSE_FUNC_SGESVD) ||
         (func == MORSE_FUNC_DGESVD) ||
         (func == MORSE_FUNC_CGESVD) ||
         (func == MORSE_FUNC_ZGESVD)))
        NT *= 2;

    lm = IB * MT;
    ln = NB * NT;

    /* Allocate and initialize descriptor */
    *desc = (MORSE_desc_t*)malloc(sizeof(MORSE_desc_t));
    if (*desc == NULL) {
        morse_error("morse_alloc_ibnb_tile", "malloc() failed");
        return MORSE_ERR_OUT_OF_RESOURCES;
    }
    /* TODO: Fix the distribution of the workspace */
    **desc = morse_desc_init(type, IB, NB, IB*NB, lm, ln, 0, 0, lm, ln, 1, 1);

    /* Allocate matrix */
    if (morse_desc_mat_alloc(*desc)) {
        morse_error("morse_alloc_ibnb_tile", "malloc() failed");
        free(*desc);
        return MORSE_ERR_OUT_OF_RESOURCES;
    }

    /* Check that everything is ok */
    status = morse_desc_check(*desc);
    if (status != MORSE_SUCCESS) {
        morse_error("morse_alloc_ibnb_tile", "invalid descriptor");
        free(*desc);
        return status;
    }

    return MORSE_SUCCESS;
}

/*******************************************************************************
 *
 **/
int morse_alloc_ipiv(int M, int N, MORSE_enum func, int type, MORSE_desc_t **desc, void **IPIV)
{
    int status;
    int NB, IB, MT, NT;
    int64_t lm, ln;
    size_t size;
    MORSE_context_t *morse;

    morse = morse_context_self();
    if (morse == NULL) {
        morse_fatal_error("morse_alloc_ipiv", "MORSE not initialized");
        return MORSE_ERR_NOT_INITIALIZED;
    }

    /* Tune NB & IB depending on M & N; Set IBNBSIZE */
    status = morse_tune(func, M, N, 0);
    if (status != MORSE_SUCCESS) {
        morse_error("morse_alloc_ipiv", "morse_tune() failed");
        return MORSE_ERR_UNEXPECTED;
    }

    /* Set MT & NT & allocate */
    NB = MORSE_NB;
    IB = MORSE_IB;

137
#if defined(CHAMELEON_USE_MAGMA)
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
/*     IB *= 2; */
#endif

    NT = (N%NB==0) ? (N/NB) : ((N/NB)+1);
    MT = (M%NB==0) ? (M/NB) : ((M/NB)+1);

    lm = IB * MT;
    ln = NB * NT;

    size = (size_t)(min(MT, NT) * NB * NT * sizeof(int));
    if (size <= 0) {
        *IPIV = NULL;
        return MORSE_SUCCESS;
    }

    *desc = (MORSE_desc_t*)malloc(sizeof(MORSE_desc_t));
    *IPIV = (int*)malloc( size );

    /* TODO: Fix the distribution for L / IPIV */
    **desc = morse_desc_init(type, IB, NB, IB*NB, lm, ln, 0, 0, lm, ln, 1, 1 );

    if ( morse_desc_mat_alloc(*desc) ) {
        morse_error("morse_alloc_ipiv", "malloc() failed");
        free(*desc);
        return MORSE_ERR_OUT_OF_RESOURCES;
    }

    return MORSE_SUCCESS;
}

/*******************************************************************************
 *
170
 * @ingroup Workspace
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
 *
 *  MORSE_Dealloc_Worksapce - Deallocate workspace descriptor allocated by
 *                            any workspace allocation routine.
 *
 *******************************************************************************
 *
 * @param[in] desc
 *          Workspace descriptor
 *
 *******************************************************************************
 *
 * @return
 *          \retval MORSE_SUCCESS successful exit
 *
 ******************************************************************************/
int MORSE_Dealloc_Workspace(MORSE_desc_t **desc)
{
    MORSE_context_t *morse;

    morse = morse_context_self();
    if (morse == NULL) {
        morse_fatal_error("MORSE_Dealloc_Workspace", "MORSE not initialized");
        return MORSE_ERR_NOT_INITIALIZED;
    }
    if (*desc == NULL) {
        morse_error("MORSE_Dealloc_Workspace", "attempting to deallocate a NULL descriptor");
        return MORSE_ERR_UNALLOCATED;
    }
199
    if ((*desc)->mat == NULL && (*desc)->use_mat == 1) {
200 201 202 203 204 205 206 207
        morse_error("MORSE_Dealloc_Worspace", "attempting to deallocate a NULL pointer");
        return MORSE_ERR_UNALLOCATED;
    }
    morse_desc_mat_free( *desc );
    free(*desc);
    *desc = NULL;
    return MORSE_SUCCESS;
}