~ubuntu-branches/ubuntu/lucid/hdf-eos5/lucid

« back to all changes in this revision

Viewing changes to testdrivers/threads/he5_sw_threads-2.c

  • Committer: Bazaar Package Importer
  • Author(s): Alastair McKinstry
  • Date: 2009-08-17 23:07:29 UTC
  • Revision ID: james.westby@ubuntu.com-20090817230729-gzbwp1ny01hv2nlk
Tags: upstream-5.1.12.dfsg.1
ImportĀ upstreamĀ versionĀ 5.1.12.dfsg.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *********************************************************
 
3
 *       FILE:     he5_sw_threads-2.c                    *                     
 
4
 *       PURPOSE:  To test thread-safe version of        *
 
5
 *                 HDF-EOS5 Swath library                *              
 
6
 *       Author:   A. Muslimov, Emergent IT Inc.         * 
 
7
 *       Date:     Sep, 2001                             *  
 
8
 *       Updated:  Nov, 2001                             *              
 
9
 *********************************************************  
 
10
 */
 
11
 
 
12
#include <HE5_HdfEosDef.h>
 
13
 
 
14
#define NUM_THREADS   30
 
15
 
 
16
typedef struct ThreadInfo_tag
 
17
{
 
18
    int      threadNum;
 
19
        hid_t    fid;
 
20
        hid_t    swid;
 
21
    char     swathName[81];
 
22
} ThreadInfo_t;
 
23
 
 
24
 
 
25
void *HE5_SwathTest(void *arg);
 
26
 
 
27
int main()
 
28
{
 
29
  herr_t           ret       = SUCCEED;
 
30
  hid_t            fid       = FAIL;
 
31
  int              loopCount;
 
32
  int              status    = FAIL;
 
33
  ThreadInfo_t     threadInfo[NUM_THREADS];
 
34
  pthread_t        threadArray[NUM_THREADS];
 
35
  pthread_attr_t   threadAttr;
 
36
  
 
37
  /* Open HDF-EOS Swath file */
 
38
  /* ----------------------- */
 
39
  fid = HE5_SWopen("swath.h5",H5F_ACC_RDWR); 
 
40
  if (fid == FAIL)
 
41
    {
 
42
          printf( "*ERROR:  unable to open \"swath.h5\"  file. \n");
 
43
          return (FAIL);
 
44
    }
 
45
  
 
46
  /* Execute the loop to the NUM_THREADS times        */
 
47
  /* to assign one function to each thread afterwards */
 
48
  /* ------------------------------------------------ */
 
49
  for (loopCount = 0; loopCount < NUM_THREADS; loopCount++)
 
50
    {
 
51
          threadInfo[loopCount].threadNum = loopCount+1;
 
52
          threadInfo[loopCount].fid       = fid;
 
53
          threadInfo[loopCount].swid      = FAIL;
 
54
          sprintf(threadInfo[loopCount].swathName,"SWATH_%d",loopCount);
 
55
          sleep(1);
 
56
          pthread_attr_init(&threadAttr);
 
57
          pthread_attr_setscope(&threadAttr,PTHREAD_SCOPE_SYSTEM);
 
58
          status = pthread_create(&threadArray[loopCount],&threadAttr,HE5_SwathTest,&threadInfo[loopCount]);
 
59
          if (status != 0)
 
60
        {
 
61
                  printf("\nERROR:  problem starting thread %d, error val: %d\n",loopCount,status);
 
62
                  ret = FAIL;
 
63
        }
 
64
          pthread_join(threadArray[loopCount],NULL); 
 
65
    }
 
66
  
 
67
  ret = HE5_SWclose(fid);
 
68
  if (ret == FAIL)
 
69
    {
 
70
          printf( "*ERROR:  unable to close file. \n");
 
71
          return (FAIL);
 
72
    }
 
73
  return(ret);
 
74
}
 
75
 
 
76
 
 
77
void *HE5_SwathTest(void *arg)
 
78
{
 
79
  int         j;
 
80
  herr_t      status = FAIL;
 
81
  hssize_t    start[3];   
 
82
  hsize_t     count[3];
 
83
  double      *plane;
 
84
  double      *outbuf;
 
85
 
 
86
  ThreadInfo_t *tag = (ThreadInfo_t *) arg;
 
87
 
 
88
  printf("Starting thread  %d\n",tag->threadNum);
 
89
  
 
90
  tag->swid = HE5_SWattach(tag->fid,tag->swathName);
 
91
  if (tag->swid == FAIL)
 
92
        {
 
93
          printf( "*ERROR:  unable to attach to %s swath. \n",tag->swathName);
 
94
          return (void *)NULL;
 
95
        }
 
96
         
 
97
  plane = (double *)calloc(12000, sizeof(double));
 
98
  if (plane == NULL)
 
99
        {
 
100
          printf( "*ERROR:  unable to allocate memory. \n");
 
101
          return (void *)NULL;
 
102
        }
 
103
 
 
104
  for (j = 0; j < 12000; j++)
 
105
        plane[j] = (double)j;
 
106
          
 
107
  start[0] = 0;      count[0] = 15;
 
108
  start[1] = 0;      count[1] = 40;
 
109
  start[2] = 0;      count[2] = 20;
 
110
  
 
111
  status = HE5_SWwritefield(tag->swid, "Spectra", start, NULL, count, plane);
 
112
  if (status == FAIL)
 
113
        {
 
114
          printf( "*ERROR:  unable to write data for \"Spectra\" field in  %s swath. \n",tag->swathName);
 
115
          return (void *)NULL;
 
116
        }
 
117
 
 
118
  free(plane);
 
119
          
 
120
  status = HE5_SWdetach(tag->swid);
 
121
  if (status == FAIL)
 
122
        {
 
123
          printf( "*ERROR:  unable to detach from %s swath. \n",tag->swathName);
 
124
          return (void *)NULL;
 
125
        }
 
126
  
 
127
  tag->swid = HE5_SWattach(tag->fid,tag->swathName);
 
128
  if (tag->swid == FAIL)
 
129
        {
 
130
          printf( "*ERROR:  unable to attach to %s swath. \n",tag->swathName);
 
131
          return (void *)NULL;
 
132
        }
 
133
 
 
134
  outbuf = (double *)calloc(12000, sizeof(double));
 
135
  if (outbuf == NULL)
 
136
        {
 
137
          printf( "*ERROR:  unable to allocate memory. \n");
 
138
          return (void *)NULL;
 
139
        }
 
140
  
 
141
  start[0] = 0;      count[0] = 15;
 
142
  start[1] = 0;      count[1] = 40;
 
143
  start[2] = 0;      count[2] = 20;
 
144
  
 
145
  status = HE5_SWreadfield(tag->swid, "Spectra", start, NULL, count, outbuf);
 
146
  if (status == FAIL)
 
147
        {
 
148
          printf( "*ERROR:  unable to read data from \"Spectra\" field in  %s swath. \n",tag->swathName);
 
149
          return (void *)NULL;
 
150
        }
 
151
  
 
152
  free(outbuf);
 
153
  
 
154
  status = HE5_SWdetach(tag->swid);
 
155
  if (status == FAIL)
 
156
        {
 
157
          printf( "*ERROR:  unable to detach from %s swath. \n",tag->swathName);
 
158
          return (void *)NULL;
 
159
        }
 
160
  
 
161
  return 0;
 
162
}
 
163
 
 
164
 
 
165
 
 
166
 
 
167
 
 
168
 
 
169
 
 
170
 
 
171