fmriPredictor.cu.prehip 5.32 KB
Newer Older
wangkx1's avatar
init  
wangkx1 committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
/*! \file fmriPredictor.cu
    \brief Contains definitions for class for making Gaussian process based predictions about fMRI data.

    \author Jesper Andersson
    \version 1.0b, May., 2022.
*/
// Definitions of class to make Gaussian-Process
// based predictions about fMRI data.
//
// fmriPredictor.cu
//
// Jesper Andersson, FMRIB Image Analysis Group
//
// Copyright (C) 2011 University of Oxford
//

#include <cstdlib>
#include <string>
#include <vector>
#include <cmath>
#pragma push
#pragma diag_suppress = code_is_unreachable // Supress warnings from armawrap
#pragma diag_suppress = expr_has_no_effect  // Supress warnings from boost
#include "armawrap/newmat.h"
#include "newimage/newimageall.h"
#pragma pop
#include "miscmaths/miscmaths.h"
#include "EddyHelperClasses.h"
#include "EddyUtils.h"
#include "fmriPredictor.h"
#include "CudaVolume.h"

using namespace EDDY;

/****************************************************************//**
* \brief Returns prediction for point given by indx
*
* Returns a predicted image for the given index, where index refers
* to the corresponding time-point.
* It should be noted that this implementation isn't very efficient as
* it reloads all image to the GPU. That means that if there are N images
* and we want to predict them all it takes N*N transfers to the GPU.
* \param index refers to the corresponding time-point
* \param exclude Decides if indx itself should be used in the prediction
* (exclude=false) or not (exclude=true)
* \param pvec Prediction vector for index
* \param pi The "predicted image"
*
********************************************************************/
void fmriPredictor::predict_image_gpu(// Input
				      unsigned int             indx,
				      bool                     exclude,
				      const arma::rowvec&      pvec,
				      // Output
				      NEWIMAGE::volume<float>& pi) const EddyTry
{
  unsigned int pvec_length = (exclude) ? this->no_of_scans_in_same_session(indx)-1 : this->no_of_scans_in_same_session(indx);
  if (pvec_length != pvec.n_cols) throw EddyException("fmriPredictor::predict_image_gpu: Wrong length pvec");

  if (!NEWIMAGE::samesize(pi,*(this->first_imptr()))) {
    pi.reinitialize(this->first_imptr()->xsize(),this->first_imptr()->ysize(),this->first_imptr()->zsize());
    NEWIMAGE::copybasicproperties(*(this->first_imptr()),pi);
  }
  EDDY::CudaVolume pcv(pi,false);
  int sindx = this->index_in_session(indx);
  std::shared_ptr<NEWIMAGE::volume<float> > ptr=nullptr;
  for (unsigned int s=0; s<this->no_of_scans_in_same_session(indx); s++) {
    if (exclude) {
      if (s < sindx) pcv.MultiplyAndAddToMe(EDDY::CudaVolume(*(this->imptr_from_same_session(indx,s))),pvec(s));
      // Do nothing if s==sindx
      else if (s > indx) pcv.MultiplyAndAddToMe(EDDY::CudaVolume(*(this->imptr_from_same_session(indx,s))),pvec(s-1));
    }
    else pcv.MultiplyAndAddToMe(EDDY::CudaVolume(*(this->imptr_from_same_session(indx,s))),pvec(s));
  }
  pcv += EDDY::CudaVolume(*(this->meanptr_from_same_session(indx)));
  pcv.GetVolume(pi);
  return;
} EddyCatch

void fmriPredictor::predict_images_gpu(// Input
				       const std::vector<unsigned int>&       indicies,
				       bool                                   exclude,
				       const std::vector<arma::rowvec>&       pvecs,
				       // Output
				       std::vector<NEWIMAGE::volume<float> >& pi) const EddyTry
{
  // Sanity checks
  if (indicies.size() != pvecs.size() || indicies.size() != pi.size()) {
    throw EDDY::EddyException("fmriPredictor::predict_images_gpu: mismatch among indicies, pvecs and pi");
  }
  if (!this->all_in_same_session(indicies)) throw EDDY::EddyException("fmriPredictor::predict_images_gpu: indicies are in different sessions");
  unsigned int pvec_length = (exclude) ? this->no_of_scans_in_same_session(indicies[0])-1 : this->no_of_scans_in_same_session(indicies[0]);
  if (std::any_of(pvecs.begin(),pvecs.end(),[pvec_length](const arma::rowvec& rv){ return(rv.n_cols != pvec_length); })) {
    throw EDDY::EddyException("fmriPredictor::predict_images_gpu: pvec with wrong length");
  }

  // Start by allocating space on GPU for all output images
  std::vector<EDDY::CudaVolume> pcvs(indicies.size());
  for (unsigned int i=0; i<indicies.size(); i++) {
    if (!NEWIMAGE::samesize(pi[i],*(this->first_imptr()))) {
      pi[i].reinitialize(this->first_imptr()->xsize(),this->first_imptr()->ysize(),this->first_imptr()->zsize());
      NEWIMAGE::copybasicproperties(*(this->first_imptr()),pi[i]);
    }
    pcvs[i].SetHdr(pi[i]);
  }
  // Transfer mean image to the GPU
  EDDY::CudaVolume mean = *(this->meanptr_from_same_session(indicies[0]));
  // Do the GP predictions
  for (unsigned int s=0; s<this->no_of_scans_in_same_session(indicies[0]); s++) { // s index into original volumes
    EDDY::CudaVolume cv = *(this->imptr_from_same_session(indicies[0],s));
    for (unsigned int i=0; i<indicies.size(); i++) { // i index into predictions
      if (exclude) {
	if (s < this->index_in_session(indicies[i])) pcvs[i].MultiplyAndAddToMe(cv,pvecs[i](s));
	// Do nothing if (s == indicies[i])
	else if (s > this->index_in_session(indicies[i])) pcvs[i].MultiplyAndAddToMe(cv,pvecs[i](s-1));
      }
      else pcvs[i].MultiplyAndAddToMe(cv,pvecs[i](s));
    }
  }
  // Add means to predictions and transfer back from GPU
  for (unsigned int i=0; i<indicies.size(); i++) {
    pcvs[i] += mean;
    pcvs[i].GetVolume(pi[i]);
  }
  return;
} EddyCatch