sym_seq_sum.cxx 12.4 KB
Newer Older
1 2 3 4 5 6 7 8
/*Copyright (c) 2011, Edgar Solomonik, all rights reserved.*/

#include "../shared/iter_tsr.h"
#include "../shared/util.h"
#include <limits.h>
#include "sym_seq_sum.h"

namespace CTF_int {
9 10

  #define SCAL_B do {                                                      \
11
    if (sr_B->isequal(beta, sr_B->mulid())){                                 \
12 13 14 15 16
      memset(idx_glb, 0, sizeof(int)*idx_max);                             \
      idx_A = 0, idx_B = 0;                                                \
      sym_pass = 1;                                                        \
      for (;;){                                                            \
        if (sym_pass){                                                     \
17
          sr_B->mul(beta, B+idx_B*sr_B->el_size, B+idx_B*sr_B->el_size);      \
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
          CTF_FLOPS_ADD(1);                                                \
        }                                                                  \
        for (idx=0; idx<idx_max; idx++){                                   \
          imin = 0, imax = INT_MAX;                                        \
          GET_MIN_MAX(B,1,2);                                              \
          if (rev_idx_map[2*idx+1] == -1) imax = imin+1;                   \
          idx_glb[idx]++;                                                  \
          if (idx_glb[idx] >= imax){                                       \
             idx_glb[idx] = imin;                                          \
          }                                                                \
          if (idx_glb[idx] != imin) {                                      \
             break;                                                        \
          }                                                                \
        }                                                                  \
        if (idx == idx_max) break;                                         \
        CHECK_SYM(B);                                                      \
        if (!sym_pass) continue;                                           \
        if (order_B > 0)                                                   \
          RESET_IDX(B);                                                    \
      }                                                                    \
    } } while (0)


  #define SCAL_B_inr do {                                                  \
42
    if (sr_B->isequal(beta, sr_B->mulid())){                                 \
43 44 45 46 47
      memset(idx_glb, 0, sizeof(int)*idx_max);                             \
      idx_A = 0, idx_B = 0;                                                \
      sym_pass = 1;                                                        \
      for (;;){                                                            \
        if (sym_pass){                                                     \
48
          sr_B->scal(inr_stride, beta, B+idx_B*inr_stride*sr_B->el_size, 1); \
49
          CTF_FLOPS_ADD(inr_stride);                                       \
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
        }                                                                  \
        for (idx=0; idx<idx_max; idx++){                                   \
          imin = 0, imax = INT_MAX;                                        \
          GET_MIN_MAX(B,1,2);                                              \
          if (rev_idx_map[2*idx+1] == -1) imax = imin+1;                   \
          idx_glb[idx]++;                                                  \
          if (idx_glb[idx] >= imax){                                       \
             idx_glb[idx] = imin;                                          \
          }                                                                \
          if (idx_glb[idx] != imin) {                                      \
             break;                                                        \
          }                                                                \
        }                                                                  \
        if (idx == idx_max) break;                                         \
        CHECK_SYM(B);                                                      \
        if (!sym_pass) continue;                                           \
        if (order_B > 0)                                                   \
          RESET_IDX(B);                                                    \
      }                                                                    \
69
    } } while (0)
70

71 72 73 74 75 76 77 78 79 80 81 82 83 84
  int sym_seq_sum_ref( char const *     alpha,
                       char const *     A,
                       algstrct const * sr_A,
                       int              order_A,
                       int const *      edge_len_A,
                       int const *      sym_A,
                       int const *      idx_map_A,
                       char const *     beta,
                       char *           B,
                       algstrct const * sr_B,
                       int              order_B,
                       int const *      edge_len_B,
                       int const *      sym_B,
                       int const *      idx_map_B){
85 86 87 88 89 90 91 92 93 94
    TAU_FSTART(sym_seq_sum_ref);
    int idx, i, idx_max, imin, imax, idx_A, idx_B, iA, iB, j, k;
    int off_idx, off_lda, sym_pass;
    int * idx_glb, * rev_idx_map;
    int * dlen_A, * dlen_B;

    inv_idx(order_A,       idx_map_A,
            order_B,       idx_map_B,
            &idx_max,     &rev_idx_map);

95 96
    dlen_A = (int*)CTF_int::alloc(sizeof(int)*order_A);
    dlen_B = (int*)CTF_int::alloc(sizeof(int)*order_B);
97 98 99
    memcpy(dlen_A, edge_len_A, sizeof(int)*order_A);
    memcpy(dlen_B, edge_len_B, sizeof(int)*order_B);

100
    idx_glb = (int*)CTF_int::alloc(sizeof(int)*idx_max);
101

102
    SCAL_B;
103 104 105 106 107 108 109 110

    memset(idx_glb, 0, sizeof(int)*idx_max);
    idx_A = 0, idx_B = 0;
    sym_pass = 1;
    for (;;){
      if (sym_pass){
    /*    printf("B[%d] = %lf*(A[%d]=%lf)+%lf*(B[%d]=%lf\n",
                idx_B,alpha,idx_A,A[idx_A],beta,idx_B,B[idx_B]);*/
111
        if (alpha != NULL){
112 113 114
          char tmp[sr_B->el_size];
          sr_B->mul(A+sr_A->el_size*idx_A, alpha, tmp);
          sr_B->add(tmp, B+sr_B->el_size*idx_B, B+sr_B->el_size*idx_B);
115 116
          CTF_FLOPS_ADD(2);
        } else {
117
          sr_B->add(A+sr_A->el_size*idx_A, B+sr_B->el_size*idx_B, B+sr_B->el_size*idx_B);
118 119
          CTF_FLOPS_ADD(1);
        }
120 121 122 123 124 125 126 127 128 129 130 131 132
      }

      for (idx=0; idx<idx_max; idx++){
        imin = 0, imax = INT_MAX;

        GET_MIN_MAX(A,0,2);
        GET_MIN_MAX(B,1,2);

        ASSERT(idx_glb[idx] >= imin && idx_glb[idx] < imax);

        idx_glb[idx]++;

        if (idx_glb[idx] >= imax){
133
          idx_glb[idx] = imin;
134 135
        }
        if (idx_glb[idx] != imin) {
136
          break;
137 138 139 140 141 142 143 144 145 146 147 148 149 150
        }
      }
      if (idx == idx_max) break;

      CHECK_SYM(A);
      if (!sym_pass) continue;
      CHECK_SYM(B);
      if (!sym_pass) continue;
      
      if (order_A > 0)
        RESET_IDX(A);
      if (order_B > 0)
        RESET_IDX(B);
    }
151 152 153 154
    CTF_int::cfree(dlen_A);
    CTF_int::cfree(dlen_B);
    CTF_int::cfree(idx_glb);
    CTF_int::cfree(rev_idx_map);
155 156 157 158
    TAU_FSTOP(sym_seq_sum_ref);
    return 0;
  }

159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
  int sym_seq_sum_inr( char const *     alpha,
                       char const *     A,
                       algstrct const * sr_A,
                       int              order_A,
                       int const *      edge_len_A,
                       int const *      sym_A,
                       int const *      idx_map_A,
                       char const *     beta,
                       char *           B,
                       algstrct const * sr_B,
                       int              order_B,
                       int const *      edge_len_B,
                       int const *      sym_B,
                       int const *      idx_map_B,
                       int              inr_stride){
174 175 176 177 178 179 180 181 182 183
    TAU_FSTART(sym_seq_sum_inr);
    int idx, i, idx_max, imin, imax, idx_A, idx_B, iA, iB, j, k;
    int off_idx, off_lda, sym_pass;
    int * idx_glb, * rev_idx_map;
    int * dlen_A, * dlen_B;

    inv_idx(order_A,       idx_map_A,
            order_B,       idx_map_B,
            &idx_max,     &rev_idx_map);

184 185
    dlen_A = (int*)CTF_int::alloc(sizeof(int)*order_A);
    dlen_B = (int*)CTF_int::alloc(sizeof(int)*order_B);
186 187 188
    memcpy(dlen_A, edge_len_A, sizeof(int)*order_A);
    memcpy(dlen_B, edge_len_B, sizeof(int)*order_B);

189
    idx_glb = (int*)CTF_int::alloc(sizeof(int)*idx_max);
190

191
    SCAL_B_inr;
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206

    memset(idx_glb, 0, sizeof(int)*idx_max);
   
    idx_A = 0, idx_B = 0;
    sym_pass = 1;
    for (;;){
      if (sym_pass){
    /*    printf("B[%d] = %lf*(A[%d]=%lf)+%lf*(B[%d]=%lf\n",
                idx_B,alpha,idx_A,A[idx_A],beta,idx_B,B[idx_B]);*/
      //  B[idx_B] = alpha*A[idx_A] + beta*B[idx_B];
    /*    if (beta != 1.0){
          cxaxpy<dtype>(inr_stride, beta-1.0, B+idx_B*inr_stride, 1, B+idx_B*inr_stride, 1);
          CTF_FLOPS_ADD(2*inr_stride);
        }*/
        //cxaxpy<dtype>(inr_stride, alpha, A+idx_A*inr_stride, 1, B+idx_B*inr_stride, 1); 
207
        sr_B->axpy(inr_stride, alpha, A+sr_A->el_size*idx_A*inr_stride, 1, B+sr_B->el_size*idx_B*inr_stride, 1); 
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
        CTF_FLOPS_ADD(2*inr_stride);
      }

      for (idx=0; idx<idx_max; idx++){
        imin = 0, imax = INT_MAX;

        GET_MIN_MAX(A,0,2);
        GET_MIN_MAX(B,1,2);

        ASSERT(idx_glb[idx] >= imin && idx_glb[idx] < imax);

        idx_glb[idx]++;

        if (idx_glb[idx] >= imax){
          idx_glb[idx] = imin;
        }
        if (idx_glb[idx] != imin) {
          break;
        }
      }
      if (idx == idx_max) break;

      CHECK_SYM(A);
      if (!sym_pass) continue;
      CHECK_SYM(B);
      if (!sym_pass) continue;
      
      if (order_A > 0)
        RESET_IDX(A);
      if (order_B > 0)
        RESET_IDX(B);
    }
240 241 242 243
    CTF_int::cfree(dlen_A);
    CTF_int::cfree(dlen_B);
    CTF_int::cfree(idx_glb);
    CTF_int::cfree(rev_idx_map);
244 245 246 247
    TAU_FSTOP(sym_seq_sum_inr);
    return 0;
  }

248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
  int sym_seq_sum_cust(char const *     alpha,
                       char const *     A,
                       algstrct const * sr_A,
                       int              order_A,
                       int const *      edge_len_A,
                       int const *      sym_A,
                       int const *      idx_map_A,
                       char const *     beta,
                       char *           B,
                       algstrct const * sr_B,
                       int              order_B,
                       int const *      edge_len_B,
                       int const *      sym_B,
                       int const *      idx_map_B,
                       univar_function  func){
263 264 265 266 267 268 269 270 271 272
    TAU_FSTART(sym_seq_sum_cust);
    int idx, i, idx_max, imin, imax, idx_A, idx_B, iA, iB, j, k;
    int off_idx, off_lda, sym_pass;
    int * idx_glb, * rev_idx_map;
    int * dlen_A, * dlen_B;

    inv_idx(order_A,       idx_map_A,
            order_B,       idx_map_B,
            &idx_max,     &rev_idx_map);

273 274
    dlen_A = (int*)CTF_int::alloc(sizeof(int)*order_A);
    dlen_B = (int*)CTF_int::alloc(sizeof(int)*order_B);
275 276 277
    memcpy(dlen_A, edge_len_A, sizeof(int)*order_A);
    memcpy(dlen_B, edge_len_B, sizeof(int)*order_B);

278
    idx_glb = (int*)CTF_int::alloc(sizeof(int)*idx_max);
279 280
    memset(idx_glb, 0, sizeof(int)*idx_max);

281
    SCAL_B;
282 283 284 285 286

    idx_A = 0, idx_B = 0;
    sym_pass = 1;
    for (;;){
      if (sym_pass){
287
        if (alpha != NULL){
288
          char tmp[sr_B->el_size];
289 290
          sr_B->mul(A+sr_A->el_size*idx_A, alpha, tmp);
          func.apply_f(tmp, tmp);
291
          sr_B->add(B+idx_B*sr_B->el_size, tmp, B+sr_B->el_size*idx_B);
292 293
          CTF_FLOPS_ADD(2);
        } else {
294
          func.apply_f(A+idx_A*sr_A->el_size, B+idx_B*sr_B->el_size);
295 296
          CTF_FLOPS_ADD(1);
        }
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
      }

      for (idx=0; idx<idx_max; idx++){
        imin = 0, imax = INT_MAX;

        GET_MIN_MAX(A,0,2);
        GET_MIN_MAX(B,1,2);

        ASSERT(idx_glb[idx] >= imin && idx_glb[idx] < imax);

        idx_glb[idx]++;

        if (idx_glb[idx] >= imax){
          idx_glb[idx] = imin;
        }
        if (idx_glb[idx] != imin) {
          break;
        }
      }
      if (idx == idx_max) break;

      CHECK_SYM(A);
      if (!sym_pass) continue;
      CHECK_SYM(B);
      if (!sym_pass) continue;
      
      if (order_A > 0)
        RESET_IDX(A);
      if (order_B > 0)
        RESET_IDX(B);
    }
328 329 330 331
    CTF_int::cfree(dlen_A);
    CTF_int::cfree(dlen_B);
    CTF_int::cfree(idx_glb);
    CTF_int::cfree(rev_idx_map);
332 333 334 335
    TAU_FSTOP(sym_seq_sum_cust);
    return 0;
  }
}