| 1 | /*BHEADER**********************************************************************
|
|---|
| 2 | * Copyright (c) 2008, Lawrence Livermore National Security, LLC.
|
|---|
| 3 | * Produced at the Lawrence Livermore National Laboratory.
|
|---|
| 4 | * This file is part of HYPRE. See file COPYRIGHT for details.
|
|---|
| 5 | *
|
|---|
| 6 | * HYPRE is free software; you can redistribute it and/or modify it under the
|
|---|
| 7 | * terms of the GNU Lesser General Public License (as published by the Free
|
|---|
| 8 | * Software Foundation) version 2.1 dated February 1999.
|
|---|
| 9 | *
|
|---|
| 10 | * $Revision: 2.4 $
|
|---|
| 11 | ***********************************************************************EHEADER*/
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 | /******************************************************************************
|
|---|
| 15 | *
|
|---|
| 16 | * Structured inner product routine
|
|---|
| 17 | *
|
|---|
| 18 | *****************************************************************************/
|
|---|
| 19 |
|
|---|
| 20 | #include "headers.h"
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 | /*--------------------------------------------------------------------------
|
|---|
| 24 | * hypre_StructInnerProd
|
|---|
| 25 | *--------------------------------------------------------------------------*/
|
|---|
| 26 |
|
|---|
| 27 | #ifdef HYPRE_USE_PTHREADS
|
|---|
| 28 | double *local_result_ref[hypre_MAX_THREADS];
|
|---|
| 29 | #endif
|
|---|
| 30 |
|
|---|
| 31 | double final_innerprod_result;
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 | double
|
|---|
| 35 | hypre_StructInnerProd( hypre_StructVector *x,
|
|---|
| 36 | hypre_StructVector *y )
|
|---|
| 37 | {
|
|---|
| 38 | double local_result;
|
|---|
| 39 | double process_result;
|
|---|
| 40 |
|
|---|
| 41 | hypre_Box *x_data_box;
|
|---|
| 42 | hypre_Box *y_data_box;
|
|---|
| 43 |
|
|---|
| 44 | int xi;
|
|---|
| 45 | int yi;
|
|---|
| 46 |
|
|---|
| 47 | double *xp;
|
|---|
| 48 | double *yp;
|
|---|
| 49 |
|
|---|
| 50 | hypre_BoxArray *boxes;
|
|---|
| 51 | hypre_Box *box;
|
|---|
| 52 | hypre_Index loop_size;
|
|---|
| 53 | hypre_IndexRef start;
|
|---|
| 54 | hypre_Index unit_stride;
|
|---|
| 55 |
|
|---|
| 56 | int i;
|
|---|
| 57 | int loopi, loopj, loopk;
|
|---|
| 58 | #ifdef HYPRE_USE_PTHREADS
|
|---|
| 59 | int threadid = hypre_GetThreadID();
|
|---|
| 60 | #endif
|
|---|
| 61 |
|
|---|
| 62 | local_result = 0.0;
|
|---|
| 63 | process_result = 0.0;
|
|---|
| 64 |
|
|---|
| 65 | hypre_SetIndex(unit_stride, 1, 1, 1);
|
|---|
| 66 |
|
|---|
| 67 | boxes = hypre_StructGridBoxes(hypre_StructVectorGrid(y));
|
|---|
| 68 | hypre_ForBoxI(i, boxes)
|
|---|
| 69 | {
|
|---|
| 70 | box = hypre_BoxArrayBox(boxes, i);
|
|---|
| 71 | start = hypre_BoxIMin(box);
|
|---|
| 72 |
|
|---|
| 73 | x_data_box = hypre_BoxArrayBox(hypre_StructVectorDataSpace(x), i);
|
|---|
| 74 | y_data_box = hypre_BoxArrayBox(hypre_StructVectorDataSpace(y), i);
|
|---|
| 75 |
|
|---|
| 76 | xp = hypre_StructVectorBoxData(x, i);
|
|---|
| 77 | yp = hypre_StructVectorBoxData(y, i);
|
|---|
| 78 |
|
|---|
| 79 | hypre_BoxGetSize(box, loop_size);
|
|---|
| 80 |
|
|---|
| 81 | #ifdef HYPRE_USE_PTHREADS
|
|---|
| 82 | local_result_ref[threadid] = &local_result;
|
|---|
| 83 | #endif
|
|---|
| 84 |
|
|---|
| 85 | hypre_BoxLoop2Begin(loop_size,
|
|---|
| 86 | x_data_box, start, unit_stride, xi,
|
|---|
| 87 | y_data_box, start, unit_stride, yi);
|
|---|
| 88 |
|
|---|
| 89 | hypre_BoxLoop2For(loopi, loopj, loopk, xi, yi)
|
|---|
| 90 | {
|
|---|
| 91 | local_result += xp[xi] * yp[yi];
|
|---|
| 92 | }
|
|---|
| 93 | hypre_BoxLoop2End(xi, yi);
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | #ifdef HYPRE_USE_PTHREADS
|
|---|
| 97 | if (threadid != hypre_NumThreads)
|
|---|
| 98 | {
|
|---|
| 99 | for (i = 0; i < hypre_NumThreads; i++)
|
|---|
| 100 | process_result += *local_result_ref[i];
|
|---|
| 101 | }
|
|---|
| 102 | else
|
|---|
| 103 | process_result = *local_result_ref[threadid];
|
|---|
| 104 | #else
|
|---|
| 105 | process_result = local_result;
|
|---|
| 106 | #endif
|
|---|
| 107 |
|
|---|
| 108 |
|
|---|
| 109 | MPI_Allreduce(&process_result, &final_innerprod_result, 1,
|
|---|
| 110 | MPI_DOUBLE, MPI_SUM, hypre_StructVectorComm(x));
|
|---|
| 111 |
|
|---|
| 112 |
|
|---|
| 113 | #ifdef HYPRE_USE_PTHREADS
|
|---|
| 114 | if (threadid == 0 || threadid == hypre_NumThreads)
|
|---|
| 115 | #endif
|
|---|
| 116 | hypre_IncFLOPCount(2*hypre_StructVectorGlobalSize(x));
|
|---|
| 117 |
|
|---|
| 118 | return final_innerprod_result;
|
|---|
| 119 | }
|
|---|