<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Objektorientiert und OpenMP?]]></title><description><![CDATA[<p>Hey Leute, ich habe da ein kleines Problem. Ich soll ein Programm schreiben das einen String-Matching Algorithmus implementiert. Es handelt sich um den Horspool falls das was sagt/hilft. Ich habe den Horspool Objektorientiert angelegt und er läuft auch Problemlos. Wobei ich glaube das er ein wenig langsam ist. Nun soll das ganze mit OMP Parallel programmiert werden. Aber irgendwie will das Programm nicht ganz so wie ich will. Ich dachte mir, ich teile die Datei in der das Pattern gesucht werden soll in die Anzahl von Teilen, wie ich Threads erstellen möchte. Jeder Teil der Datei(Thread) ist ein eigenes Objekt. Dann rufe ich über eine for-Schleife für jedes Objekt eine Funktion des Objektes auf in der der Horspool eigentlich durchgeführt wird. Allerdings will das ganze einfach nicht parallel laufen. Ich bekomme immer die gleiche Zeit. Könnt ihr mir bitte helfen?<br />
Ich hänge euch mal meinen Code mit an.</p>
<pre><code class="language-cpp">#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;vector&gt;
#include &lt;algorithm&gt;
#include &lt;iostream&gt;
#include &lt;fstream&gt;
#include &lt;map&gt;
#include &lt;omp.h&gt;
using namespace std;
ifstream::pos_type size;

//For Horspool       
class Search {
                static const int MAXCHAR = 256;
				map&lt;char,int&gt; d;
                int m;
                char* patt;
                int count;
                vector&lt;int&gt;hits ;
        public:
                Search(char*);
                int find(char*,int,int);
                int get_count();
                vector&lt;int&gt; get_hits();
};

int Search::get_count()
{
  return count;
}        

vector &lt;int&gt; Search::get_hits()
{
   return hits;
}

Search::Search(char* p)
        {

			count = 0;
			patt = p;
                m = strlen(patt);

                int k = 0;

		 for (k = 0; k &lt; MAXCHAR; k++)
		      d[k] = m;

                for (k = 0; k &lt; m - 1; k++){
		  if(d.find(patt[k])==d.end()||d[patt[k&rsqb;&rsqb;&gt;m-k-1)
		    d[patt[k&rsqb;&rsqb; = m - k - 1;
		}      
        }

int Search::find(char* text,int start,int end)
{
	if(start&gt;0)
		start +=1;

	  int n =end; 

	  if (m &gt; end-start)
			return 0;

      int k = start+(m - 1);

		while (k &lt; n) {

		        int j = m - 1;
                        int i = k;

			 while (j &gt;= 0 &amp;&amp; text[i] == patt[j]) {
                                j--;
                                i--;
                        }

                        if (j &lt; 0)
			  {
			    hits.push_back(i + 1);
			    count++;
			  }

			  k += d[text[k&rsqb;&rsqb;;

         }

		return 0;

}

//Load File
bool loadText(vector&lt;char&gt; &amp;text, char* fileName)
{
    // open file in binary and read-only mode 
    // and set file pointer to the end
    ifstream file(fileName, ios::in | ios::binary | ios::ate);
    if (!file.is_open())
        return false;

    // file pointer position is equal to file size
    // ifstream::pos_type 
size = file.tellg();
     // resize string buffer
    text.resize(size);

    // go to file begin and read complete file
    file.seekg(0, ios::beg);
    file.read(&amp;text[0], size);
    file.close();

    return true;
}
bool createFile(vector&lt;int&gt; &amp;hits, char* filename ){

  ofstream outfile;
  outfile.open(filename, ios::out | ios::trunc);
  if(!outfile.is_open())
	  return false;
  for(int i=0; i&lt;hits.size(); ++i)
	   outfile &lt;&lt; (hits[i])&lt;&lt;endl;

  outfile.close();
  return true;
}

int main(int argc, char *argv[]) {

if (argc &lt; 5)
    {
        cerr &lt;&lt; argv[0] &lt;&lt; &quot; &lt;TEXTFILE&gt; &lt;PATTERN 1&gt; &lt;NMBR OF THREADS&gt; &lt;OUTPUTFILE&gt;&quot; &lt;&lt; endl;
        return 0;
    }

	vector&lt;char&gt; text;
    if (!loadText(text, argv[1]))
    {
        cerr &lt;&lt; &quot;Could not open text file.&quot; &lt;&lt; endl;
        return 1;
    }

    vector&lt;Search*&gt; SearchObjects;
    int nmbr_of_threads = atoi(argv[3]);
    vector&lt;int&gt; cuts;

    for(int h=0;h&lt;=nmbr_of_threads;h++)
      {
		cuts.push_back((int)(size*((double)((double)h/(double)nmbr_of_threads))));
      }

    for(int h=0;h&lt;2*nmbr_of_threads-1;h++)
      {
	Search *newSearch = new Search(argv[2]);
	SearchObjects.push_back(newSearch);
      }

    for(int h=1;h&lt;cuts.size()-1;h++)
      {
	vector&lt;int&gt; check;

	Search *newSearch = new Search(argv[2]);

	newSearch-&gt;find(&amp;text[0],cuts[h]-(strlen(argv[2])-1),cuts[h]+(strlen(argv[2])-1));
	check = newSearch-&gt;get_hits();
	if(!check.empty()){
		cuts[h]=check[0];}

      }

 double start = omp_get_wtime();

#pragma omp parallel num_threads(nmbr_of_threads) shared(SearchObjects,text,cuts)
{
#pragma omp for schedule(static,1) nowait
	for(int h=0;h&lt;nmbr_of_threads;h++)
      { 
		SearchObjects[h]-&gt;find(&amp;text[0],cuts[h],cuts[h+1]);
      }
}

    double end = omp_get_wtime();
    vector&lt;int&gt;all_hits;
    int all_counts = 0;
    for(int h=0;h&lt;nmbr_of_threads;h++)
      {
	vector&lt;int&gt;hits=SearchObjects[h]-&gt;get_hits();
	for(int hi=0;hi&lt;hits.size();hi++)
	  all_hits.push_back(hits[hi]);
	all_counts+=SearchObjects[h]-&gt;get_count();
      }
       cout&lt;&lt;&quot;\nHorspool:\t&quot;&lt;&lt;all_counts&lt;&lt;&quot; hits found in &quot;&lt;&lt;(int)((end-start)*1000)&lt;&lt;&quot;ms&quot;&lt;&lt;endl;

if (!createFile(all_hits, argv[4]))
    {
        cerr &lt;&lt; &quot;Could not open output file.&quot; &lt;&lt; endl;
        return 1;
    }
 return 0;
}
</code></pre>
<p>MfG Monchichu</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/296749/objektorientiert-und-openmp</link><generator>RSS for Node</generator><lastBuildDate>Fri, 14 Aug 2026 17:55:45 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/296749.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 11 Dec 2011 17:18:49 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Objektorientiert und OpenMP? on Sun, 11 Dec 2011 20:28:40 GMT]]></title><description><![CDATA[<p>Hey Leute, ich habe da ein kleines Problem. Ich soll ein Programm schreiben das einen String-Matching Algorithmus implementiert. Es handelt sich um den Horspool falls das was sagt/hilft. Ich habe den Horspool Objektorientiert angelegt und er läuft auch Problemlos. Wobei ich glaube das er ein wenig langsam ist. Nun soll das ganze mit OMP Parallel programmiert werden. Aber irgendwie will das Programm nicht ganz so wie ich will. Ich dachte mir, ich teile die Datei in der das Pattern gesucht werden soll in die Anzahl von Teilen, wie ich Threads erstellen möchte. Jeder Teil der Datei(Thread) ist ein eigenes Objekt. Dann rufe ich über eine for-Schleife für jedes Objekt eine Funktion des Objektes auf in der der Horspool eigentlich durchgeführt wird. Allerdings will das ganze einfach nicht parallel laufen. Ich bekomme immer die gleiche Zeit. Könnt ihr mir bitte helfen?<br />
Ich hänge euch mal meinen Code mit an.</p>
<pre><code class="language-cpp">#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;vector&gt;
#include &lt;algorithm&gt;
#include &lt;iostream&gt;
#include &lt;fstream&gt;
#include &lt;map&gt;
#include &lt;omp.h&gt;
using namespace std;
ifstream::pos_type size;

//For Horspool       
class Search {
                static const int MAXCHAR = 256;
				map&lt;char,int&gt; d;
                int m;
                char* patt;
                int count;
                vector&lt;int&gt;hits ;
        public:
                Search(char*);
                int find(char*,int,int);
                int get_count();
                vector&lt;int&gt; get_hits();
};

int Search::get_count()
{
  return count;
}        

vector &lt;int&gt; Search::get_hits()
{
   return hits;
}

Search::Search(char* p)
        {

			count = 0;
			patt = p;
                m = strlen(patt);

                int k = 0;

		 for (k = 0; k &lt; MAXCHAR; k++)
		      d[k] = m;

                for (k = 0; k &lt; m - 1; k++){
		  if(d.find(patt[k])==d.end()||d[patt[k&rsqb;&rsqb;&gt;m-k-1)
		    d[patt[k&rsqb;&rsqb; = m - k - 1;
		}      
        }

int Search::find(char* text,int start,int end)
{
	if(start&gt;0)
		start +=1;

	  int n =end; 

	  if (m &gt; end-start)
			return 0;

      int k = start+(m - 1);

		while (k &lt; n) {

		        int j = m - 1;
                        int i = k;

			 while (j &gt;= 0 &amp;&amp; text[i] == patt[j]) {
                                j--;
                                i--;
                        }

                        if (j &lt; 0)
			  {
			    hits.push_back(i + 1);
			    count++;
			  }

			  k += d[text[k&rsqb;&rsqb;;

         }

		return 0;

}

//Load File
bool loadText(vector&lt;char&gt; &amp;text, char* fileName)
{
    // open file in binary and read-only mode 
    // and set file pointer to the end
    ifstream file(fileName, ios::in | ios::binary | ios::ate);
    if (!file.is_open())
        return false;

    // file pointer position is equal to file size
    // ifstream::pos_type 
size = file.tellg();
     // resize string buffer
    text.resize(size);

    // go to file begin and read complete file
    file.seekg(0, ios::beg);
    file.read(&amp;text[0], size);
    file.close();

    return true;
}
bool createFile(vector&lt;int&gt; &amp;hits, char* filename ){

  ofstream outfile;
  outfile.open(filename, ios::out | ios::trunc);
  if(!outfile.is_open())
	  return false;
  for(int i=0; i&lt;hits.size(); ++i)
	   outfile &lt;&lt; (hits[i])&lt;&lt;endl;

  outfile.close();
  return true;
}

int main(int argc, char *argv[]) {

if (argc &lt; 5)
    {
        cerr &lt;&lt; argv[0] &lt;&lt; &quot; &lt;TEXTFILE&gt; &lt;PATTERN 1&gt; &lt;NMBR OF THREADS&gt; &lt;OUTPUTFILE&gt;&quot; &lt;&lt; endl;
        return 0;
    }

	vector&lt;char&gt; text;
    if (!loadText(text, argv[1]))
    {
        cerr &lt;&lt; &quot;Could not open text file.&quot; &lt;&lt; endl;
        return 1;
    }

    vector&lt;Search*&gt; SearchObjects;
    int nmbr_of_threads = atoi(argv[3]);
    vector&lt;int&gt; cuts;

    for(int h=0;h&lt;=nmbr_of_threads;h++)
      {
		cuts.push_back((int)(size*((double)((double)h/(double)nmbr_of_threads))));
      }

    for(int h=0;h&lt;2*nmbr_of_threads-1;h++)
      {
	Search *newSearch = new Search(argv[2]);
	SearchObjects.push_back(newSearch);
      }

    for(int h=1;h&lt;cuts.size()-1;h++)
      {
	vector&lt;int&gt; check;

	Search *newSearch = new Search(argv[2]);

	newSearch-&gt;find(&amp;text[0],cuts[h]-(strlen(argv[2])-1),cuts[h]+(strlen(argv[2])-1));
	check = newSearch-&gt;get_hits();
	if(!check.empty()){
		cuts[h]=check[0];}

      }

 double start = omp_get_wtime();

#pragma omp parallel num_threads(nmbr_of_threads) shared(SearchObjects,text,cuts)
{
#pragma omp for schedule(static,1) nowait
	for(int h=0;h&lt;nmbr_of_threads;h++)
      { 
		SearchObjects[h]-&gt;find(&amp;text[0],cuts[h],cuts[h+1]);
      }
}

    double end = omp_get_wtime();
    vector&lt;int&gt;all_hits;
    int all_counts = 0;
    for(int h=0;h&lt;nmbr_of_threads;h++)
      {
	vector&lt;int&gt;hits=SearchObjects[h]-&gt;get_hits();
	for(int hi=0;hi&lt;hits.size();hi++)
	  all_hits.push_back(hits[hi]);
	all_counts+=SearchObjects[h]-&gt;get_count();
      }
       cout&lt;&lt;&quot;\nHorspool:\t&quot;&lt;&lt;all_counts&lt;&lt;&quot; hits found in &quot;&lt;&lt;(int)((end-start)*1000)&lt;&lt;&quot;ms&quot;&lt;&lt;endl;

if (!createFile(all_hits, argv[4]))
    {
        cerr &lt;&lt; &quot;Could not open output file.&quot; &lt;&lt; endl;
        return 1;
    }
 return 0;
}
</code></pre>
<p>MfG Monchichu</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2155802</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2155802</guid><dc:creator><![CDATA[Monchichu]]></dc:creator><pubDate>Sun, 11 Dec 2011 20:28:40 GMT</pubDate></item><item><title><![CDATA[Reply to Objektorientiert und OpenMP? on Mon, 12 Dec 2011 06:56:55 GMT]]></title><description><![CDATA[<p>Lies dir mal bitte den Link in meiner Signatur durch - vor allem den Teil mit &quot;aufs Wesentliche reduzieren&quot;. Zu deinem Problem: sagt der Compiler beim Übersetzen was zum #pragma omp? Musst du ggf. noch irgendeinen Parameter beim compilieren mitgeben? Das ist sehr compilerspezifisch, da OMP kein Standard-C++ ist.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2156016</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2156016</guid><dc:creator><![CDATA[pumuckl]]></dc:creator><pubDate>Mon, 12 Dec 2011 06:56:55 GMT</pubDate></item><item><title><![CDATA[Reply to Objektorientiert und OpenMP? on Mon, 12 Dec 2011 10:46:33 GMT]]></title><description><![CDATA[<p>Ich kompiliere auf einem Linux mit dem Befehl g++ -Wall -fopenmp xxx.cpp -o xxx und Fehler bekomme ich keine nur unsigned int warnings welche jedoch vernachlässigt werden können.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2156101</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2156101</guid><dc:creator><![CDATA[Monchichu]]></dc:creator><pubDate>Mon, 12 Dec 2011 10:46:33 GMT</pubDate></item><item><title><![CDATA[Reply to Objektorientiert und OpenMP? on Mon, 12 Dec 2011 11:21:45 GMT]]></title><description><![CDATA[<p>Funktioniert ganz hervorragend bei mir:</p>
<pre><code>[b]$&gt;[/b] g++ test.cc -Wall -Wextra -O3
test.cc: In function ‘bool createFile(std::vector&lt;int, std::allocator&lt;int&gt; &gt;&amp;, char*)’:
test.cc:120: warning: comparison between signed and unsigned integer expressions
test.cc: In function ‘int main(int, char**)’:
test.cc:159: warning: comparison between signed and unsigned integer expressions
test.cc:189: warning: comparison between signed and unsigned integer expressions
[b]$&gt;[/b] dd if=/dev/urandom of=test.dat bs=10000 count=10000
10000+0 records in
10000+0 records out
100000000 bytes (100 MB) copied, 12.7066 s, 7.9 MB/s
[b]$&gt;[/b] ./a.out  test.dat test 1 test.result
Horspool:	0 hits found in 1273ms
[b]$&gt;[/b] ./a.out  test.dat test 2 test.result
Horspool:	0 hits found in 664ms
[b]$&gt;[/b] ./a.out  test.dat test 3 test.result
Horspool:	0 hits found in 443ms
[b]$&gt;[/b] ./a.out  test.dat test 4 test.result
Horspool:	0 hits found in 332ms
</code></pre>
<p>Einziger Unterschied der mir auffällt zu deinem Versuch ist, dass ich getreu dem Motto &quot;niemanden interessiert es, wie schnell dein unoptimierter Code läuft&quot; gehandelt habe.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2156107</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2156107</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Mon, 12 Dec 2011 11:21:45 GMT</pubDate></item><item><title><![CDATA[Reply to Objektorientiert und OpenMP? on Mon, 12 Dec 2011 11:24:52 GMT]]></title><description><![CDATA[<p>Das &quot;ganz hervorragend&quot; sollte man noch einschränken: <strong>Dein Programm ist ein einziges großes Speicherloch.</strong> Warum nutzt du soviel new und Pointergefrickel, wenn du es noch nicht einmal kannst und wenn man es in C++ sowieso nicht braucht und wenn doch, dann ganz anders?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2156109</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2156109</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Mon, 12 Dec 2011 11:24:52 GMT</pubDate></item></channel></rss>