1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements.  See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership.  The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License.  You may obtain a copy of the License at
9  *
10  *   http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied.  See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
20 #include "mshadow/tensor.h"
21 #include "old/tensor.h"
22 #include "assert.h"
23 #include <cstring>
24 
25 using mshadow::index_t;
26 template<typename T>
Print(T const & ist)27 void Print(T const & ist) {
28   for (int i = 0; i < ist.size(0); ++i) {
29     for (int j = 0; j < ist.size(1); ++j) {
30       printf("%.2f ", ist[i][j]);
31     }
32     printf("\n");
33   }
34 }
35 
Check(mshadow::TensorContainer<mshadow::cpu,2,float> & mct,Xmshadow::TensorContainer<Xmshadow::cpu,2> & xct)36 bool Check(mshadow::TensorContainer<mshadow::cpu, 2, float> &mct, \
37            Xmshadow::TensorContainer<Xmshadow::cpu, 2> &xct) {
38   for (index_t i = 0; i < mct.size(0); ++i) {
39     for (index_t j = 0; j < mct.size(1); ++j) {
40       assert(mct[i][j] == xct[i][j]);
41     }
42   }
43   return true;
44 }
45 
46 template<typename xpua, typename xpub>
RunTask()47 void RunTask() {
48   const int X = 6;
49   const int K = 2;
50   mshadow::TensorContainer<mshadow::cpu, 2, float> srcm(mshadow::Shape2(X, X));
51   Xmshadow::TensorContainer<Xmshadow::cpu, 2> srcx(Xmshadow::Shape2(X, X));
52 
53   mshadow::TensorContainer<xpua, 2, float> mct(mshadow::Shape2(X, X));
54   Xmshadow::TensorContainer<xpub, 2> xct(Xmshadow::Shape2(X, X));
55   for (int i = 0; i < X; ++i) {
56     for (int j = 0; j < X; ++j) {
57       srcm[i][j] = i * 0.1f + j * 0.1f;
58       srcx[i][j] = i * 0.1f + j * 0.1f;
59     }
60   }
61   mshadow::Copy(mct, srcm);
62   Xmshadow::Copy(xct, srcx);
63   mshadow::TensorContainer<xpua, 2, float> pool_ct(mshadow::Shape2((X-K)/2+1, (X-K)/2+1));
64   Xmshadow::TensorContainer<xpub, 2> pool_xct(Xmshadow::Shape2((X-K)/2+1, (X-K)/2+1));
65 
66   pool_ct = mshadow::expr::pool<mshadow::red::maximum>(mct, K, K, K);
67   pool_xct = Xmshadow::expr::pool<Xmshadow::red::maximum>(xct, K, K);
68 
69   mshadow::TensorContainer<mshadow::cpu, 2, float> cpool_ct(mshadow::Shape2((X-K)/2+1, (X-K)/2+1));
70   Xmshadow::TensorContainer<Xmshadow::cpu, 2> cpool_xct(Xmshadow::Shape2((X-K)/2+1, (X-K)/2+1));
71   mshadow::Copy(cpool_ct, pool_ct);
72   Xmshadow::Copy(cpool_xct, pool_xct);
73   if (Check(cpool_ct, cpool_xct)) {
74     printf("Pass\n");
75   }
76 }
77 
main(int argc,char ** argv)78 int main(int argc, char** argv) {
79   if (argc < 2) {
80     printf("Usage: dev\n");
81     exit(-1);
82   }
83   if (!strcmp(argv[1], "cpu")) {
84     RunTask<mshadow::cpu, Xmshadow::cpu>();
85   } else {
86     RunTask<mshadow::gpu, Xmshadow::gpu>();
87   }
88 }
89