C++ Random Access Iterators

Random Access Iterators for C++ in OpenMP 3.0

In OpenMP Specification Version 3.0, the control variable of a loop associated with a for directive can be a variable of random access iterator type. See Section 2.5.1 on p. 38 of the OpenMP 3.0 Specification.

Example (C++):

#include <vector>
#include <iostream>
#include <omp.h>

using namespace std;

void iterator_example(int n)
{
  std::vector<int> vec;
  std::vector<int>::iterator it;

  for (int i = 0; i < n; ++i) {
    vec .push_back(i+n);
  }

  #pragma omp parallel for shared(vec) ordered
  for (it = vec.begin(); it < vec.end(); it++) {
    #pragma omp ordered
    cout << *it << ' ';
  }
  cout << endl;
}

int main(void)
  {
    omp_set_dynamic(0);
    omp_set_num_threads(4);
    iterator_example(100);
    return 0;
  }


% CC -xopenmp -xO3 iterator_loop.cc
% a.out
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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
192 193 194 195 196 197 198 199

Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.

Sign up or Log in to add a comment or watch this page.


The individuals who post here are part of the extended Sun Microsystems community and they might not be employed or in any way formally affiliated with Sun Microsystems. The opinions expressed here are their own, are not necessarily reviewed in advance by anyone but the individual authors, and neither Sun nor any other party necessarily agrees with them.

Copyright 1994-2009 Sun Microsystems, Inc.
Powered by Atlassian Confluence
Sun Guidelines on Public Discourse Privacy Policy Terms of Use Trademarks Site Map Employment Investor Relations Contact