1 /*************************************************************************/
2 /* Copyright (c) 2014,2018 Linas Vepstas                                 */
3 /* All rights reserved                                                   */
4 /*                                                                       */
5 /* Use of the link grammar parsing system is subject to the terms of the */
6 /* license set forth in the LICENSE file included with this software.    */
7 /* This license allows free redistribution and use in source and binary  */
8 /* forms, with or without modification, subject to certain conditions.   */
9 /*                                                                       */
10 /*************************************************************************/
11 
12 // This implements a very simple-minded multi-threaded unit test.
13 // All it does is to make sure the system doesn't crash e.g. due to
14 // memory allocation conflicts.
15 
16 #include <thread>
17 #include <vector>
18 
19 #include <locale.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include "link-grammar/link-includes.h"
23 
parse_one_sent(const char * sent_str)24 static void parse_one_sent(const char *sent_str)
25 {
26 	Parse_Options opts = parse_options_create();
27 	// Dictionary dict = dictionary_create_lang("ru");
28 	Dictionary dict = dictionary_create_lang("en");
29 	if (!dict) {
30 		fprintf (stderr, "Fatal error: Unable to open the dictionary\n");
31 		exit(1);
32 	}
33 
34 	Sentence sent = sentence_create(sent_str, dict);
35 	if (!sent) {
36 		fprintf (stderr, "Fatal error: Unable to create parser\n");
37 		exit(2);
38 	}
39 
40 	sentence_split(sent, opts);
41 	int num_linkages = sentence_parse(sent, opts);
42 	if (num_linkages <= 0) {
43 		fprintf (stderr, "Fatal error: Unable to parse sentence\n");
44 		exit(3);
45 	}
46 
47 	if (2 < num_linkages) num_linkages = 2;
48 	for (int li = 0; li<num_linkages; li++)
49 	{
50 		Linkage linkage = linkage_create(li, sent, opts);
51 		linkage_delete(linkage);
52 	}
53 	sentence_delete(sent);
54 
55 	dictionary_delete(dict);
56 	parse_options_delete(opts);
57 }
58 
parse_sents(int thread_id,int niter)59 static void parse_sents(int thread_id, int niter)
60 {
61 	const char *sents[] = {
62 		"Frank felt vindicated when his long time friend Bill revealed that he was the winner of the competition.",
63 		"Logorrhea, or excessive and often incoherent talkativeness or wordiness, is a social disease.",
64 		"It was covered with bites.",
65 		"I have no idea what that is.",
66 		"His shout had been involuntary, something anybody might have done.",
67 		"Trump, Ryan and McConnell are using the budget process to pay for the GOP’s $1.5 trillion tax scam.",
68 		"We ate popcorn and watched movies on TV for three days.",
69 		"Sweat stood on his brow, fury was bright in his one good eye.",
70 		"One of the things you do when you stop your bicycle is apply the brake.",
71 		"The line extends 10 miles offshore."
72 // "под броню боевого робота устремились потоки энергии.",
73 // "через четверть часа здесь будет полно полицейских."
74 	};
75 
76 	int nsents = sizeof(sents) / sizeof(const char *);
77 
78 	for (int j=0; j<niter; j += nsents)
79 	{
80 		for (int i=0; i < nsents; ++i)
81 		{
82 			parse_one_sent(sents[i]);
83 		}
84 	}
85 }
86 
main(int argc,char * argv[])87 int main(int argc, char* argv[])
88 {
89 	setlocale(LC_ALL, "en_US.UTF-8");
90 	dictionary_set_data_dir(DICTIONARY_DIR "/data");
91 
92 	int n_threads = 10;
93 	int niter = 30;
94 
95 	printf("Creating %d threads, each parsing %d sentences\n",
96 		 n_threads, niter);
97 	std::vector<std::thread> thread_pool;
98 	for (int i=0; i < n_threads; i++) {
99 		thread_pool.push_back(std::thread(parse_sents, i, niter));
100 	}
101 
102 	// Wait for all threads to complete
103 	for (std::thread& t : thread_pool) t.join();
104 	printf("Done with multi-threaded parsing\n");
105 
106 	return 0;
107 }
108