workspace.c 5.78 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
int morse_alloc_ibnb_tile(int M, int N, MORSE_enum func, int type, MORSE_desc_t **desc, int p, int q)
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
{
    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;
    }
88
    **desc = morse_desc_init(type, IB, NB, IB*NB, lm, ln, 0, 0, lm, ln, p, q);
89 90 91 92 93 94 95 96

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

97 98
    RUNTIME_desc_create( *desc );

99 100 101 102 103 104 105 106 107 108 109 110 111 112
    /* 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;
}

/*******************************************************************************
 *
 **/
113
int morse_alloc_ipiv(int M, int N, MORSE_enum func, int type, MORSE_desc_t **desc, void **IPIV, int p, int q)
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
{
    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;

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

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

144
    size = (size_t)(chameleon_min(MT, NT) * NB * NT * sizeof(int));
145 146 147 148
    if (size <= 0) {
        *IPIV = NULL;
        return MORSE_SUCCESS;
    }
149
    /* TODO: Fix the distribution for IPIV */
150 151
    *IPIV = (int*)malloc( size );

152 153
    *desc = (MORSE_desc_t*)malloc(sizeof(MORSE_desc_t));
    **desc = morse_desc_init(type, IB, NB, IB*NB, lm, ln, 0, 0, lm, ln, p, q );
154 155 156 157 158 159 160

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

161 162
    RUNTIME_desc_create( *desc );

163 164 165 166 167
    return MORSE_SUCCESS;
}

/*******************************************************************************
 *
168
 * @ingroup Workspace
169 170 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
 *
 *  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;
    }
197
    if ((*desc)->mat == NULL && (*desc)->use_mat == 1) {
198 199 200 201 202 203 204 205
        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;
}